Clone of mesa.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

radeon_drm_winsys.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Copyright © 2009 Corbin Simpson
  3. * Copyright © 2011 Marek Olšák <maraeo@gmail.com>
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
  18. * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  21. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * The above copyright notice and this permission notice (including the
  24. * next paragraph) shall be included in all copies or substantial portions
  25. * of the Software.
  26. */
  27. /*
  28. * Authors:
  29. * Corbin Simpson <MostAwesomeDude@gmail.com>
  30. * Joakim Sindholt <opensource@zhasha.com>
  31. * Marek Olšák <maraeo@gmail.com>
  32. */
  33. #include "radeon_drm_bo.h"
  34. #include "radeon_drm_cs.h"
  35. #include "radeon_drm_public.h"
  36. #include "pipebuffer/pb_bufmgr.h"
  37. #include "util/u_memory.h"
  38. #include <xf86drm.h>
  39. #include <stdio.h>
  40. /*
  41. * this are copy from radeon_drm, once an updated libdrm is released
  42. * we should bump configure.ac requirement for it and remove the following
  43. * field
  44. */
  45. #ifndef RADEON_INFO_TILING_CONFIG
  46. #define RADEON_INFO_TILING_CONFIG 6
  47. #endif
  48. #ifndef RADEON_INFO_WANT_HYPERZ
  49. #define RADEON_INFO_WANT_HYPERZ 7
  50. #endif
  51. #ifndef RADEON_INFO_WANT_CMASK
  52. #define RADEON_INFO_WANT_CMASK 8
  53. #endif
  54. #ifndef RADEON_INFO_CLOCK_CRYSTAL_FREQ
  55. #define RADEON_INFO_CLOCK_CRYSTAL_FREQ 9
  56. #endif
  57. #ifndef RADEON_INFO_NUM_BACKENDS
  58. #define RADEON_INFO_NUM_BACKENDS 0xa
  59. #endif
  60. #ifndef RADEON_INFO_NUM_TILE_PIPES
  61. #define RADEON_INFO_NUM_TILE_PIPES 0xb
  62. #endif
  63. #ifndef RADEON_INFO_BACKEND_MAP
  64. #define RADEON_INFO_BACKEND_MAP 0xd
  65. #endif
  66. #ifndef RADEON_INFO_VA_START
  67. /* virtual address start, va < start are reserved by the kernel */
  68. #define RADEON_INFO_VA_START 0x0e
  69. /* maximum size of ib using the virtual memory cs */
  70. #define RADEON_INFO_IB_VM_MAX_SIZE 0x0f
  71. #endif
  72. /* Enable/disable feature access for one command stream.
  73. * If enable == TRUE, return TRUE on success.
  74. * Otherwise, return FALSE.
  75. *
  76. * We basically do the same thing kernel does, because we have to deal
  77. * with multiple contexts (here command streams) backed by one winsys. */
  78. static boolean radeon_set_fd_access(struct radeon_drm_cs *applier,
  79. struct radeon_drm_cs **owner,
  80. pipe_mutex *mutex,
  81. unsigned request, boolean enable)
  82. {
  83. struct drm_radeon_info info;
  84. unsigned value = enable ? 1 : 0;
  85. memset(&info, 0, sizeof(info));
  86. pipe_mutex_lock(*mutex);
  87. /* Early exit if we are sure the request will fail. */
  88. if (enable) {
  89. if (*owner) {
  90. pipe_mutex_unlock(*mutex);
  91. return FALSE;
  92. }
  93. } else {
  94. if (*owner != applier) {
  95. pipe_mutex_unlock(*mutex);
  96. return FALSE;
  97. }
  98. }
  99. /* Pass through the request to the kernel. */
  100. info.value = (unsigned long)&value;
  101. info.request = request;
  102. if (drmCommandWriteRead(applier->ws->fd, DRM_RADEON_INFO,
  103. &info, sizeof(info)) != 0) {
  104. pipe_mutex_unlock(*mutex);
  105. return FALSE;
  106. }
  107. /* Update the rights in the winsys. */
  108. if (enable) {
  109. if (value) {
  110. *owner = applier;
  111. fprintf(stderr, "radeon: Acquired Hyper-Z.\n");
  112. pipe_mutex_unlock(*mutex);
  113. return TRUE;
  114. }
  115. } else {
  116. *owner = NULL;
  117. fprintf(stderr, "radeon: Released Hyper-Z.\n");
  118. }
  119. pipe_mutex_unlock(*mutex);
  120. return FALSE;
  121. }
  122. static boolean radeon_get_drm_value(int fd, unsigned request,
  123. const char *errname, uint32_t *out)
  124. {
  125. struct drm_radeon_info info;
  126. int retval;
  127. memset(&info, 0, sizeof(info));
  128. info.value = (unsigned long)out;
  129. info.request = request;
  130. retval = drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, sizeof(info));
  131. if (retval) {
  132. if (errname) {
  133. fprintf(stderr, "radeon: Failed to get %s, error number %d\n",
  134. errname, retval);
  135. }
  136. return FALSE;
  137. }
  138. return TRUE;
  139. }
  140. /* Helper function to do the ioctls needed for setup and init. */
  141. static boolean do_winsys_init(struct radeon_drm_winsys *ws)
  142. {
  143. struct drm_radeon_gem_info gem_info;
  144. int retval;
  145. drmVersionPtr version;
  146. memset(&gem_info, 0, sizeof(gem_info));
  147. /* We do things in a specific order here.
  148. *
  149. * DRM version first. We need to be sure we're running on a KMS chipset.
  150. * This is also for some features.
  151. *
  152. * Then, the PCI ID. This is essential and should return usable numbers
  153. * for all Radeons. If this fails, we probably got handed an FD for some
  154. * non-Radeon card.
  155. *
  156. * The GEM info is actually bogus on the kernel side, as well as our side
  157. * (see radeon_gem_info_ioctl in radeon_gem.c) but that's alright because
  158. * we don't actually use the info for anything yet.
  159. *
  160. * The GB and Z pipe requests should always succeed, but they might not
  161. * return sensical values for all chipsets, but that's alright because
  162. * the pipe drivers already know that.
  163. */
  164. /* Get DRM version. */
  165. version = drmGetVersion(ws->fd);
  166. if (version->version_major != 2 ||
  167. version->version_minor < 3) {
  168. fprintf(stderr, "%s: DRM version is %d.%d.%d but this driver is "
  169. "only compatible with 2.3.x (kernel 2.6.34) or later.\n",
  170. __FUNCTION__,
  171. version->version_major,
  172. version->version_minor,
  173. version->version_patchlevel);
  174. drmFreeVersion(version);
  175. return FALSE;
  176. }
  177. ws->info.drm_major = version->version_major;
  178. ws->info.drm_minor = version->version_minor;
  179. ws->info.drm_patchlevel = version->version_patchlevel;
  180. drmFreeVersion(version);
  181. /* Get PCI ID. */
  182. if (!radeon_get_drm_value(ws->fd, RADEON_INFO_DEVICE_ID, "PCI ID",
  183. &ws->info.pci_id))
  184. return FALSE;
  185. /* Check PCI ID. */
  186. switch (ws->info.pci_id) {
  187. #define CHIPSET(pci_id, name, family) case pci_id:
  188. #include "pci_ids/r300_pci_ids.h"
  189. #undef CHIPSET
  190. ws->gen = R300;
  191. break;
  192. #define CHIPSET(pci_id, name, family) case pci_id:
  193. #include "pci_ids/r600_pci_ids.h"
  194. #undef CHIPSET
  195. ws->gen = R600;
  196. break;
  197. default:
  198. fprintf(stderr, "radeon: Invalid PCI ID.\n");
  199. return FALSE;
  200. }
  201. /* Get GEM info. */
  202. retval = drmCommandWriteRead(ws->fd, DRM_RADEON_GEM_INFO,
  203. &gem_info, sizeof(gem_info));
  204. if (retval) {
  205. fprintf(stderr, "radeon: Failed to get MM info, error number %d\n",
  206. retval);
  207. return FALSE;
  208. }
  209. ws->info.gart_size = gem_info.gart_size;
  210. ws->info.vram_size = gem_info.vram_size;
  211. ws->num_cpus = sysconf(_SC_NPROCESSORS_ONLN);
  212. /* Generation-specific queries. */
  213. if (ws->gen == R300) {
  214. if (!radeon_get_drm_value(ws->fd, RADEON_INFO_NUM_GB_PIPES,
  215. "GB pipe count",
  216. &ws->info.r300_num_gb_pipes))
  217. return FALSE;
  218. if (!radeon_get_drm_value(ws->fd, RADEON_INFO_NUM_Z_PIPES,
  219. "Z pipe count",
  220. &ws->info.r300_num_z_pipes))
  221. return FALSE;
  222. }
  223. else if (ws->gen == R600) {
  224. if (ws->info.drm_minor >= 9 &&
  225. !radeon_get_drm_value(ws->fd, RADEON_INFO_NUM_BACKENDS,
  226. "num backends",
  227. &ws->info.r600_num_backends))
  228. return FALSE;
  229. /* get the GPU counter frequency, failure is not fatal */
  230. radeon_get_drm_value(ws->fd, RADEON_INFO_CLOCK_CRYSTAL_FREQ, NULL,
  231. &ws->info.r600_clock_crystal_freq);
  232. radeon_get_drm_value(ws->fd, RADEON_INFO_TILING_CONFIG, NULL,
  233. &ws->info.r600_tiling_config);
  234. if (ws->info.drm_minor >= 11) {
  235. radeon_get_drm_value(ws->fd, RADEON_INFO_NUM_TILE_PIPES, NULL,
  236. &ws->info.r600_num_tile_pipes);
  237. if (radeon_get_drm_value(ws->fd, RADEON_INFO_BACKEND_MAP, NULL,
  238. &ws->info.r600_backend_map))
  239. ws->info.r600_backend_map_valid = TRUE;
  240. }
  241. ws->info.r600_virtual_address = FALSE;
  242. if (ws->info.drm_minor >= 13) {
  243. ws->info.r600_virtual_address = TRUE;
  244. if (!radeon_get_drm_value(ws->fd, RADEON_INFO_VA_START, NULL,
  245. &ws->info.r600_va_start))
  246. ws->info.r600_virtual_address = FALSE;
  247. if (!radeon_get_drm_value(ws->fd, RADEON_INFO_IB_VM_MAX_SIZE, NULL,
  248. &ws->info.r600_ib_vm_max_size))
  249. ws->info.r600_virtual_address = FALSE;
  250. }
  251. }
  252. return TRUE;
  253. }
  254. static void radeon_winsys_destroy(struct radeon_winsys *rws)
  255. {
  256. struct radeon_drm_winsys *ws = (struct radeon_drm_winsys*)rws;
  257. pipe_mutex_destroy(ws->hyperz_owner_mutex);
  258. pipe_mutex_destroy(ws->cmask_owner_mutex);
  259. ws->cman->destroy(ws->cman);
  260. ws->kman->destroy(ws->kman);
  261. radeon_surface_manager_free(ws->surf_man);
  262. FREE(rws);
  263. }
  264. static void radeon_query_info(struct radeon_winsys *rws,
  265. struct radeon_info *info)
  266. {
  267. *info = ((struct radeon_drm_winsys *)rws)->info;
  268. }
  269. static boolean radeon_cs_request_feature(struct radeon_winsys_cs *rcs,
  270. enum radeon_feature_id fid,
  271. boolean enable)
  272. {
  273. struct radeon_drm_cs *cs = radeon_drm_cs(rcs);
  274. switch (fid) {
  275. case RADEON_FID_R300_HYPERZ_ACCESS:
  276. if (debug_get_bool_option("RADEON_HYPERZ", FALSE)) {
  277. return radeon_set_fd_access(cs, &cs->ws->hyperz_owner,
  278. &cs->ws->hyperz_owner_mutex,
  279. RADEON_INFO_WANT_HYPERZ, enable);
  280. } else {
  281. return FALSE;
  282. }
  283. case RADEON_FID_R300_CMASK_ACCESS:
  284. if (debug_get_bool_option("RADEON_CMASK", FALSE)) {
  285. return radeon_set_fd_access(cs, &cs->ws->cmask_owner,
  286. &cs->ws->cmask_owner_mutex,
  287. RADEON_INFO_WANT_CMASK, enable);
  288. } else {
  289. return FALSE;
  290. }
  291. }
  292. return FALSE;
  293. }
  294. static int radeon_drm_winsys_surface_init(struct radeon_winsys *rws,
  295. struct radeon_surface *surf)
  296. {
  297. struct radeon_drm_winsys *ws = (struct radeon_drm_winsys*)rws;
  298. return radeon_surface_init(ws->surf_man, surf);
  299. }
  300. static int radeon_drm_winsys_surface_best(struct radeon_winsys *rws,
  301. struct radeon_surface *surf)
  302. {
  303. struct radeon_drm_winsys *ws = (struct radeon_drm_winsys*)rws;
  304. return radeon_surface_best(ws->surf_man, surf);
  305. }
  306. struct radeon_winsys *radeon_drm_winsys_create(int fd)
  307. {
  308. struct radeon_drm_winsys *ws = CALLOC_STRUCT(radeon_drm_winsys);
  309. if (!ws) {
  310. return NULL;
  311. }
  312. ws->fd = fd;
  313. if (!do_winsys_init(ws))
  314. goto fail;
  315. /* Create managers. */
  316. ws->kman = radeon_bomgr_create(ws);
  317. if (!ws->kman)
  318. goto fail;
  319. ws->cman = pb_cache_manager_create(ws->kman, 1000000);
  320. if (!ws->cman)
  321. goto fail;
  322. /* FIXME check for libdrm version ?? */
  323. ws->surf_man = radeon_surface_manager_new(fd);
  324. if (!ws->surf_man)
  325. goto fail;
  326. /* Set functions. */
  327. ws->base.destroy = radeon_winsys_destroy;
  328. ws->base.query_info = radeon_query_info;
  329. ws->base.cs_request_feature = radeon_cs_request_feature;
  330. ws->base.surface_init = radeon_drm_winsys_surface_init;
  331. ws->base.surface_best = radeon_drm_winsys_surface_best;
  332. radeon_bomgr_init_functions(ws);
  333. radeon_drm_cs_init_functions(ws);
  334. pipe_mutex_init(ws->hyperz_owner_mutex);
  335. pipe_mutex_init(ws->cmask_owner_mutex);
  336. return &ws->base;
  337. fail:
  338. if (ws->cman)
  339. ws->cman->destroy(ws->cman);
  340. if (ws->kman)
  341. ws->kman->destroy(ws->kman);
  342. if (ws->surf_man)
  343. radeon_surface_manager_free(ws->surf_man);
  344. FREE(ws);
  345. return NULL;
  346. }