nouveau_object.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. /*
  2. * Copyright (C) 2006 Ben Skeggs.
  3. *
  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, sublicense, 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 above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial
  16. * portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  21. * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
  22. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. */
  27. /*
  28. * Authors:
  29. * Ben Skeggs <darktama@iinet.net.au>
  30. */
  31. #include "drmP.h"
  32. #include "drm.h"
  33. #include "nouveau_drv.h"
  34. #include "nouveau_drm.h"
  35. #include "nouveau_ramht.h"
  36. /* NVidia uses context objects to drive drawing operations.
  37. Context objects can be selected into 8 subchannels in the FIFO,
  38. and then used via DMA command buffers.
  39. A context object is referenced by a user defined handle (CARD32). The HW
  40. looks up graphics objects in a hash table in the instance RAM.
  41. An entry in the hash table consists of 2 CARD32. The first CARD32 contains
  42. the handle, the second one a bitfield, that contains the address of the
  43. object in instance RAM.
  44. The format of the second CARD32 seems to be:
  45. NV4 to NV30:
  46. 15: 0 instance_addr >> 4
  47. 17:16 engine (here uses 1 = graphics)
  48. 28:24 channel id (here uses 0)
  49. 31 valid (use 1)
  50. NV40:
  51. 15: 0 instance_addr >> 4 (maybe 19-0)
  52. 21:20 engine (here uses 1 = graphics)
  53. I'm unsure about the other bits, but using 0 seems to work.
  54. The key into the hash table depends on the object handle and channel id and
  55. is given as:
  56. */
  57. int
  58. nouveau_gpuobj_new(struct drm_device *dev, struct nouveau_channel *chan,
  59. uint32_t size, int align, uint32_t flags,
  60. struct nouveau_gpuobj **gpuobj_ret)
  61. {
  62. struct drm_nouveau_private *dev_priv = dev->dev_private;
  63. struct nouveau_engine *engine = &dev_priv->engine;
  64. struct nouveau_gpuobj *gpuobj;
  65. struct drm_mm_node *ramin = NULL;
  66. int ret;
  67. NV_DEBUG(dev, "ch%d size=%u align=%d flags=0x%08x\n",
  68. chan ? chan->id : -1, size, align, flags);
  69. if (!dev_priv || !gpuobj_ret || *gpuobj_ret != NULL)
  70. return -EINVAL;
  71. gpuobj = kzalloc(sizeof(*gpuobj), GFP_KERNEL);
  72. if (!gpuobj)
  73. return -ENOMEM;
  74. NV_DEBUG(dev, "gpuobj %p\n", gpuobj);
  75. gpuobj->dev = dev;
  76. gpuobj->flags = flags;
  77. gpuobj->refcount = 1;
  78. gpuobj->size = size;
  79. list_add_tail(&gpuobj->list, &dev_priv->gpuobj_list);
  80. if (chan) {
  81. NV_DEBUG(dev, "channel heap\n");
  82. ramin = drm_mm_search_free(&chan->ramin_heap, size, align, 0);
  83. if (ramin)
  84. ramin = drm_mm_get_block(ramin, size, align);
  85. if (!ramin) {
  86. nouveau_gpuobj_ref(NULL, &gpuobj);
  87. return -ENOMEM;
  88. }
  89. } else {
  90. NV_DEBUG(dev, "global heap\n");
  91. /* allocate backing pages, sets vinst */
  92. ret = engine->instmem.populate(dev, gpuobj, &size);
  93. if (ret) {
  94. nouveau_gpuobj_ref(NULL, &gpuobj);
  95. return ret;
  96. }
  97. /* try and get aperture space */
  98. ramin = drm_mm_search_free(&dev_priv->ramin_heap, size, align, 0);
  99. if (ramin)
  100. ramin = drm_mm_get_block(ramin, size, align);
  101. /* on nv50 it's ok to fail, we have a fallback path */
  102. if (!ramin && dev_priv->card_type < NV_50) {
  103. nouveau_gpuobj_ref(NULL, &gpuobj);
  104. return -ENOMEM;
  105. }
  106. }
  107. /* if we got a chunk of the aperture, map pages into it */
  108. gpuobj->im_pramin = ramin;
  109. if (!chan && gpuobj->im_pramin) {
  110. ret = engine->instmem.bind(dev, gpuobj);
  111. if (ret) {
  112. nouveau_gpuobj_ref(NULL, &gpuobj);
  113. return ret;
  114. }
  115. }
  116. /* calculate the various different addresses for the object */
  117. if (chan) {
  118. gpuobj->pinst = chan->ramin->pinst;
  119. if (gpuobj->pinst != ~0)
  120. gpuobj->pinst += gpuobj->im_pramin->start;
  121. if (dev_priv->card_type < NV_50) {
  122. gpuobj->cinst = gpuobj->pinst;
  123. } else {
  124. gpuobj->cinst = gpuobj->im_pramin->start;
  125. gpuobj->vinst = gpuobj->im_pramin->start +
  126. chan->ramin->vinst;
  127. }
  128. } else {
  129. if (gpuobj->im_pramin)
  130. gpuobj->pinst = gpuobj->im_pramin->start;
  131. else
  132. gpuobj->pinst = ~0;
  133. gpuobj->cinst = 0xdeadbeef;
  134. }
  135. if (gpuobj->flags & NVOBJ_FLAG_ZERO_ALLOC) {
  136. int i;
  137. for (i = 0; i < gpuobj->size; i += 4)
  138. nv_wo32(gpuobj, i, 0);
  139. engine->instmem.flush(dev);
  140. }
  141. *gpuobj_ret = gpuobj;
  142. return 0;
  143. }
  144. int
  145. nouveau_gpuobj_early_init(struct drm_device *dev)
  146. {
  147. struct drm_nouveau_private *dev_priv = dev->dev_private;
  148. NV_DEBUG(dev, "\n");
  149. INIT_LIST_HEAD(&dev_priv->gpuobj_list);
  150. spin_lock_init(&dev_priv->ramin_lock);
  151. dev_priv->ramin_base = ~0;
  152. return 0;
  153. }
  154. int
  155. nouveau_gpuobj_init(struct drm_device *dev)
  156. {
  157. struct drm_nouveau_private *dev_priv = dev->dev_private;
  158. struct nouveau_gpuobj *ramht = NULL;
  159. int ret;
  160. NV_DEBUG(dev, "\n");
  161. if (dev_priv->card_type >= NV_50)
  162. return 0;
  163. ret = nouveau_gpuobj_new_fake(dev, dev_priv->ramht_offset, ~0,
  164. dev_priv->ramht_size,
  165. NVOBJ_FLAG_ZERO_ALLOC, &ramht);
  166. if (ret)
  167. return ret;
  168. ret = nouveau_ramht_new(dev, ramht, &dev_priv->ramht);
  169. nouveau_gpuobj_ref(NULL, &ramht);
  170. return ret;
  171. }
  172. void
  173. nouveau_gpuobj_takedown(struct drm_device *dev)
  174. {
  175. struct drm_nouveau_private *dev_priv = dev->dev_private;
  176. NV_DEBUG(dev, "\n");
  177. nouveau_ramht_ref(NULL, &dev_priv->ramht, NULL);
  178. }
  179. void
  180. nouveau_gpuobj_late_takedown(struct drm_device *dev)
  181. {
  182. struct drm_nouveau_private *dev_priv = dev->dev_private;
  183. struct nouveau_gpuobj *gpuobj = NULL;
  184. struct list_head *entry, *tmp;
  185. NV_DEBUG(dev, "\n");
  186. list_for_each_safe(entry, tmp, &dev_priv->gpuobj_list) {
  187. gpuobj = list_entry(entry, struct nouveau_gpuobj, list);
  188. NV_ERROR(dev, "gpuobj %p still exists at takedown, refs=%d\n",
  189. gpuobj, gpuobj->refcount);
  190. gpuobj->refcount = 1;
  191. nouveau_gpuobj_ref(NULL, &gpuobj);
  192. }
  193. }
  194. static int
  195. nouveau_gpuobj_del(struct nouveau_gpuobj *gpuobj)
  196. {
  197. struct drm_device *dev = gpuobj->dev;
  198. struct drm_nouveau_private *dev_priv = dev->dev_private;
  199. struct nouveau_engine *engine = &dev_priv->engine;
  200. int i;
  201. NV_DEBUG(dev, "gpuobj %p\n", gpuobj);
  202. if (gpuobj->im_pramin && (gpuobj->flags & NVOBJ_FLAG_ZERO_FREE)) {
  203. for (i = 0; i < gpuobj->size; i += 4)
  204. nv_wo32(gpuobj, i, 0);
  205. engine->instmem.flush(dev);
  206. }
  207. if (gpuobj->dtor)
  208. gpuobj->dtor(dev, gpuobj);
  209. if (gpuobj->im_backing)
  210. engine->instmem.clear(dev, gpuobj);
  211. if (gpuobj->im_pramin)
  212. drm_mm_put_block(gpuobj->im_pramin);
  213. list_del(&gpuobj->list);
  214. kfree(gpuobj);
  215. return 0;
  216. }
  217. void
  218. nouveau_gpuobj_ref(struct nouveau_gpuobj *ref, struct nouveau_gpuobj **ptr)
  219. {
  220. if (ref)
  221. ref->refcount++;
  222. if (*ptr && --(*ptr)->refcount == 0)
  223. nouveau_gpuobj_del(*ptr);
  224. *ptr = ref;
  225. }
  226. int
  227. nouveau_gpuobj_new_fake(struct drm_device *dev, u32 pinst, u64 vinst,
  228. u32 size, u32 flags, struct nouveau_gpuobj **pgpuobj)
  229. {
  230. struct drm_nouveau_private *dev_priv = dev->dev_private;
  231. struct nouveau_gpuobj *gpuobj = NULL;
  232. int i;
  233. NV_DEBUG(dev,
  234. "pinst=0x%08x vinst=0x%010llx size=0x%08x flags=0x%08x\n",
  235. pinst, vinst, size, flags);
  236. gpuobj = kzalloc(sizeof(*gpuobj), GFP_KERNEL);
  237. if (!gpuobj)
  238. return -ENOMEM;
  239. NV_DEBUG(dev, "gpuobj %p\n", gpuobj);
  240. gpuobj->dev = dev;
  241. gpuobj->flags = flags;
  242. gpuobj->refcount = 1;
  243. gpuobj->size = size;
  244. gpuobj->pinst = pinst;
  245. gpuobj->cinst = 0xdeadbeef;
  246. gpuobj->vinst = vinst;
  247. if (gpuobj->flags & NVOBJ_FLAG_ZERO_ALLOC) {
  248. for (i = 0; i < gpuobj->size; i += 4)
  249. nv_wo32(gpuobj, i, 0);
  250. dev_priv->engine.instmem.flush(dev);
  251. }
  252. list_add_tail(&gpuobj->list, &dev_priv->gpuobj_list);
  253. *pgpuobj = gpuobj;
  254. return 0;
  255. }
  256. static uint32_t
  257. nouveau_gpuobj_class_instmem_size(struct drm_device *dev, int class)
  258. {
  259. struct drm_nouveau_private *dev_priv = dev->dev_private;
  260. /*XXX: dodgy hack for now */
  261. if (dev_priv->card_type >= NV_50)
  262. return 24;
  263. if (dev_priv->card_type >= NV_40)
  264. return 32;
  265. return 16;
  266. }
  267. /*
  268. DMA objects are used to reference a piece of memory in the
  269. framebuffer, PCI or AGP address space. Each object is 16 bytes big
  270. and looks as follows:
  271. entry[0]
  272. 11:0 class (seems like I can always use 0 here)
  273. 12 page table present?
  274. 13 page entry linear?
  275. 15:14 access: 0 rw, 1 ro, 2 wo
  276. 17:16 target: 0 NV memory, 1 NV memory tiled, 2 PCI, 3 AGP
  277. 31:20 dma adjust (bits 0-11 of the address)
  278. entry[1]
  279. dma limit (size of transfer)
  280. entry[X]
  281. 1 0 readonly, 1 readwrite
  282. 31:12 dma frame address of the page (bits 12-31 of the address)
  283. entry[N]
  284. page table terminator, same value as the first pte, as does nvidia
  285. rivatv uses 0xffffffff
  286. Non linear page tables need a list of frame addresses afterwards,
  287. the rivatv project has some info on this.
  288. The method below creates a DMA object in instance RAM and returns a handle
  289. to it that can be used to set up context objects.
  290. */
  291. int
  292. nouveau_gpuobj_dma_new(struct nouveau_channel *chan, int class,
  293. uint64_t offset, uint64_t size, int access,
  294. int target, struct nouveau_gpuobj **gpuobj)
  295. {
  296. struct drm_device *dev = chan->dev;
  297. struct drm_nouveau_private *dev_priv = dev->dev_private;
  298. struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
  299. int ret;
  300. NV_DEBUG(dev, "ch%d class=0x%04x offset=0x%llx size=0x%llx\n",
  301. chan->id, class, offset, size);
  302. NV_DEBUG(dev, "access=%d target=%d\n", access, target);
  303. switch (target) {
  304. case NV_DMA_TARGET_AGP:
  305. offset += dev_priv->gart_info.aper_base;
  306. break;
  307. default:
  308. break;
  309. }
  310. ret = nouveau_gpuobj_new(dev, chan,
  311. nouveau_gpuobj_class_instmem_size(dev, class),
  312. 16, NVOBJ_FLAG_ZERO_ALLOC |
  313. NVOBJ_FLAG_ZERO_FREE, gpuobj);
  314. if (ret) {
  315. NV_ERROR(dev, "Error creating gpuobj: %d\n", ret);
  316. return ret;
  317. }
  318. if (dev_priv->card_type < NV_50) {
  319. uint32_t frame, adjust, pte_flags = 0;
  320. if (access != NV_DMA_ACCESS_RO)
  321. pte_flags |= (1<<1);
  322. adjust = offset & 0x00000fff;
  323. frame = offset & ~0x00000fff;
  324. nv_wo32(*gpuobj, 0, ((1<<12) | (1<<13) | (adjust << 20) |
  325. (access << 14) | (target << 16) |
  326. class));
  327. nv_wo32(*gpuobj, 4, size - 1);
  328. nv_wo32(*gpuobj, 8, frame | pte_flags);
  329. nv_wo32(*gpuobj, 12, frame | pte_flags);
  330. } else {
  331. uint64_t limit = offset + size - 1;
  332. uint32_t flags0, flags5;
  333. if (target == NV_DMA_TARGET_VIDMEM) {
  334. flags0 = 0x00190000;
  335. flags5 = 0x00010000;
  336. } else {
  337. flags0 = 0x7fc00000;
  338. flags5 = 0x00080000;
  339. }
  340. nv_wo32(*gpuobj, 0, flags0 | class);
  341. nv_wo32(*gpuobj, 4, lower_32_bits(limit));
  342. nv_wo32(*gpuobj, 8, lower_32_bits(offset));
  343. nv_wo32(*gpuobj, 12, ((upper_32_bits(limit) & 0xff) << 24) |
  344. (upper_32_bits(offset) & 0xff));
  345. nv_wo32(*gpuobj, 20, flags5);
  346. }
  347. instmem->flush(dev);
  348. (*gpuobj)->engine = NVOBJ_ENGINE_SW;
  349. (*gpuobj)->class = class;
  350. return 0;
  351. }
  352. int
  353. nouveau_gpuobj_gart_dma_new(struct nouveau_channel *chan,
  354. uint64_t offset, uint64_t size, int access,
  355. struct nouveau_gpuobj **gpuobj,
  356. uint32_t *o_ret)
  357. {
  358. struct drm_device *dev = chan->dev;
  359. struct drm_nouveau_private *dev_priv = dev->dev_private;
  360. int ret;
  361. if (dev_priv->gart_info.type == NOUVEAU_GART_AGP ||
  362. (dev_priv->card_type >= NV_50 &&
  363. dev_priv->gart_info.type == NOUVEAU_GART_SGDMA)) {
  364. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
  365. offset + dev_priv->vm_gart_base,
  366. size, access, NV_DMA_TARGET_AGP,
  367. gpuobj);
  368. if (o_ret)
  369. *o_ret = 0;
  370. } else
  371. if (dev_priv->gart_info.type == NOUVEAU_GART_SGDMA) {
  372. nouveau_gpuobj_ref(dev_priv->gart_info.sg_ctxdma, gpuobj);
  373. if (offset & ~0xffffffffULL) {
  374. NV_ERROR(dev, "obj offset exceeds 32-bits\n");
  375. return -EINVAL;
  376. }
  377. if (o_ret)
  378. *o_ret = (uint32_t)offset;
  379. ret = (*gpuobj != NULL) ? 0 : -EINVAL;
  380. } else {
  381. NV_ERROR(dev, "Invalid GART type %d\n", dev_priv->gart_info.type);
  382. return -EINVAL;
  383. }
  384. return ret;
  385. }
  386. /* Context objects in the instance RAM have the following structure.
  387. * On NV40 they are 32 byte long, on NV30 and smaller 16 bytes.
  388. NV4 - NV30:
  389. entry[0]
  390. 11:0 class
  391. 12 chroma key enable
  392. 13 user clip enable
  393. 14 swizzle enable
  394. 17:15 patch config:
  395. scrcopy_and, rop_and, blend_and, scrcopy, srccopy_pre, blend_pre
  396. 18 synchronize enable
  397. 19 endian: 1 big, 0 little
  398. 21:20 dither mode
  399. 23 single step enable
  400. 24 patch status: 0 invalid, 1 valid
  401. 25 context_surface 0: 1 valid
  402. 26 context surface 1: 1 valid
  403. 27 context pattern: 1 valid
  404. 28 context rop: 1 valid
  405. 29,30 context beta, beta4
  406. entry[1]
  407. 7:0 mono format
  408. 15:8 color format
  409. 31:16 notify instance address
  410. entry[2]
  411. 15:0 dma 0 instance address
  412. 31:16 dma 1 instance address
  413. entry[3]
  414. dma method traps
  415. NV40:
  416. No idea what the exact format is. Here's what can be deducted:
  417. entry[0]:
  418. 11:0 class (maybe uses more bits here?)
  419. 17 user clip enable
  420. 21:19 patch config
  421. 25 patch status valid ?
  422. entry[1]:
  423. 15:0 DMA notifier (maybe 20:0)
  424. entry[2]:
  425. 15:0 DMA 0 instance (maybe 20:0)
  426. 24 big endian
  427. entry[3]:
  428. 15:0 DMA 1 instance (maybe 20:0)
  429. entry[4]:
  430. entry[5]:
  431. set to 0?
  432. */
  433. int
  434. nouveau_gpuobj_gr_new(struct nouveau_channel *chan, int class,
  435. struct nouveau_gpuobj **gpuobj)
  436. {
  437. struct drm_device *dev = chan->dev;
  438. struct drm_nouveau_private *dev_priv = dev->dev_private;
  439. int ret;
  440. NV_DEBUG(dev, "ch%d class=0x%04x\n", chan->id, class);
  441. ret = nouveau_gpuobj_new(dev, chan,
  442. nouveau_gpuobj_class_instmem_size(dev, class),
  443. 16,
  444. NVOBJ_FLAG_ZERO_ALLOC | NVOBJ_FLAG_ZERO_FREE,
  445. gpuobj);
  446. if (ret) {
  447. NV_ERROR(dev, "Error creating gpuobj: %d\n", ret);
  448. return ret;
  449. }
  450. if (dev_priv->card_type >= NV_50) {
  451. nv_wo32(*gpuobj, 0, class);
  452. nv_wo32(*gpuobj, 20, 0x00010000);
  453. } else {
  454. switch (class) {
  455. case NV_CLASS_NULL:
  456. nv_wo32(*gpuobj, 0, 0x00001030);
  457. nv_wo32(*gpuobj, 4, 0xFFFFFFFF);
  458. break;
  459. default:
  460. if (dev_priv->card_type >= NV_40) {
  461. nv_wo32(*gpuobj, 0, class);
  462. #ifdef __BIG_ENDIAN
  463. nv_wo32(*gpuobj, 8, 0x01000000);
  464. #endif
  465. } else {
  466. #ifdef __BIG_ENDIAN
  467. nv_wo32(*gpuobj, 0, class | 0x00080000);
  468. #else
  469. nv_wo32(*gpuobj, 0, class);
  470. #endif
  471. }
  472. }
  473. }
  474. dev_priv->engine.instmem.flush(dev);
  475. (*gpuobj)->engine = NVOBJ_ENGINE_GR;
  476. (*gpuobj)->class = class;
  477. return 0;
  478. }
  479. int
  480. nouveau_gpuobj_sw_new(struct nouveau_channel *chan, int class,
  481. struct nouveau_gpuobj **gpuobj_ret)
  482. {
  483. struct drm_nouveau_private *dev_priv;
  484. struct nouveau_gpuobj *gpuobj;
  485. if (!chan || !gpuobj_ret || *gpuobj_ret != NULL)
  486. return -EINVAL;
  487. dev_priv = chan->dev->dev_private;
  488. gpuobj = kzalloc(sizeof(*gpuobj), GFP_KERNEL);
  489. if (!gpuobj)
  490. return -ENOMEM;
  491. gpuobj->dev = chan->dev;
  492. gpuobj->engine = NVOBJ_ENGINE_SW;
  493. gpuobj->class = class;
  494. gpuobj->refcount = 1;
  495. gpuobj->cinst = 0x40;
  496. list_add_tail(&gpuobj->list, &dev_priv->gpuobj_list);
  497. *gpuobj_ret = gpuobj;
  498. return 0;
  499. }
  500. static int
  501. nouveau_gpuobj_channel_init_pramin(struct nouveau_channel *chan)
  502. {
  503. struct drm_device *dev = chan->dev;
  504. struct drm_nouveau_private *dev_priv = dev->dev_private;
  505. uint32_t size;
  506. uint32_t base;
  507. int ret;
  508. NV_DEBUG(dev, "ch%d\n", chan->id);
  509. /* Base amount for object storage (4KiB enough?) */
  510. size = 0x1000;
  511. base = 0;
  512. /* PGRAPH context */
  513. size += dev_priv->engine.graph.grctx_size;
  514. if (dev_priv->card_type == NV_50) {
  515. /* Various fixed table thingos */
  516. size += 0x1400; /* mostly unknown stuff */
  517. size += 0x4000; /* vm pd */
  518. base = 0x6000;
  519. /* RAMHT, not sure about setting size yet, 32KiB to be safe */
  520. size += 0x8000;
  521. /* RAMFC */
  522. size += 0x1000;
  523. }
  524. ret = nouveau_gpuobj_new(dev, NULL, size, 0x1000, 0, &chan->ramin);
  525. if (ret) {
  526. NV_ERROR(dev, "Error allocating channel PRAMIN: %d\n", ret);
  527. return ret;
  528. }
  529. ret = drm_mm_init(&chan->ramin_heap, base, size);
  530. if (ret) {
  531. NV_ERROR(dev, "Error creating PRAMIN heap: %d\n", ret);
  532. nouveau_gpuobj_ref(NULL, &chan->ramin);
  533. return ret;
  534. }
  535. return 0;
  536. }
  537. int
  538. nouveau_gpuobj_channel_init(struct nouveau_channel *chan,
  539. uint32_t vram_h, uint32_t tt_h)
  540. {
  541. struct drm_device *dev = chan->dev;
  542. struct drm_nouveau_private *dev_priv = dev->dev_private;
  543. struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
  544. struct nouveau_gpuobj *vram = NULL, *tt = NULL;
  545. int ret, i;
  546. NV_DEBUG(dev, "ch%d vram=0x%08x tt=0x%08x\n", chan->id, vram_h, tt_h);
  547. /* Allocate a chunk of memory for per-channel object storage */
  548. ret = nouveau_gpuobj_channel_init_pramin(chan);
  549. if (ret) {
  550. NV_ERROR(dev, "init pramin\n");
  551. return ret;
  552. }
  553. /* NV50 VM
  554. * - Allocate per-channel page-directory
  555. * - Map GART and VRAM into the channel's address space at the
  556. * locations determined during init.
  557. */
  558. if (dev_priv->card_type >= NV_50) {
  559. u32 pgd_offs = (dev_priv->chipset == 0x50) ? 0x1400 : 0x0200;
  560. u64 vm_vinst = chan->ramin->vinst + pgd_offs;
  561. u32 vm_pinst = chan->ramin->pinst;
  562. u32 pde;
  563. if (vm_pinst != ~0)
  564. vm_pinst += pgd_offs;
  565. ret = nouveau_gpuobj_new_fake(dev, vm_pinst, vm_vinst, 0x4000,
  566. 0, &chan->vm_pd);
  567. if (ret)
  568. return ret;
  569. for (i = 0; i < 0x4000; i += 8) {
  570. nv_wo32(chan->vm_pd, i + 0, 0x00000000);
  571. nv_wo32(chan->vm_pd, i + 4, 0xdeadcafe);
  572. }
  573. nouveau_gpuobj_ref(dev_priv->gart_info.sg_ctxdma,
  574. &chan->vm_gart_pt);
  575. pde = (dev_priv->vm_gart_base / (512*1024*1024)) * 8;
  576. nv_wo32(chan->vm_pd, pde + 0, chan->vm_gart_pt->vinst | 3);
  577. nv_wo32(chan->vm_pd, pde + 4, 0x00000000);
  578. pde = (dev_priv->vm_vram_base / (512*1024*1024)) * 8;
  579. for (i = 0; i < dev_priv->vm_vram_pt_nr; i++) {
  580. nouveau_gpuobj_ref(dev_priv->vm_vram_pt[i],
  581. &chan->vm_vram_pt[i]);
  582. nv_wo32(chan->vm_pd, pde + 0,
  583. chan->vm_vram_pt[i]->vinst | 0x61);
  584. nv_wo32(chan->vm_pd, pde + 4, 0x00000000);
  585. pde += 8;
  586. }
  587. instmem->flush(dev);
  588. }
  589. /* RAMHT */
  590. if (dev_priv->card_type < NV_50) {
  591. nouveau_ramht_ref(dev_priv->ramht, &chan->ramht, NULL);
  592. } else {
  593. struct nouveau_gpuobj *ramht = NULL;
  594. ret = nouveau_gpuobj_new(dev, chan, 0x8000, 16,
  595. NVOBJ_FLAG_ZERO_ALLOC, &ramht);
  596. if (ret)
  597. return ret;
  598. ret = nouveau_ramht_new(dev, ramht, &chan->ramht);
  599. nouveau_gpuobj_ref(NULL, &ramht);
  600. if (ret)
  601. return ret;
  602. }
  603. /* VRAM ctxdma */
  604. if (dev_priv->card_type >= NV_50) {
  605. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
  606. 0, dev_priv->vm_end,
  607. NV_DMA_ACCESS_RW,
  608. NV_DMA_TARGET_AGP, &vram);
  609. if (ret) {
  610. NV_ERROR(dev, "Error creating VRAM ctxdma: %d\n", ret);
  611. return ret;
  612. }
  613. } else {
  614. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
  615. 0, dev_priv->fb_available_size,
  616. NV_DMA_ACCESS_RW,
  617. NV_DMA_TARGET_VIDMEM, &vram);
  618. if (ret) {
  619. NV_ERROR(dev, "Error creating VRAM ctxdma: %d\n", ret);
  620. return ret;
  621. }
  622. }
  623. ret = nouveau_ramht_insert(chan, vram_h, vram);
  624. nouveau_gpuobj_ref(NULL, &vram);
  625. if (ret) {
  626. NV_ERROR(dev, "Error adding VRAM ctxdma to RAMHT: %d\n", ret);
  627. return ret;
  628. }
  629. /* TT memory ctxdma */
  630. if (dev_priv->card_type >= NV_50) {
  631. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
  632. 0, dev_priv->vm_end,
  633. NV_DMA_ACCESS_RW,
  634. NV_DMA_TARGET_AGP, &tt);
  635. if (ret) {
  636. NV_ERROR(dev, "Error creating VRAM ctxdma: %d\n", ret);
  637. return ret;
  638. }
  639. } else
  640. if (dev_priv->gart_info.type != NOUVEAU_GART_NONE) {
  641. ret = nouveau_gpuobj_gart_dma_new(chan, 0,
  642. dev_priv->gart_info.aper_size,
  643. NV_DMA_ACCESS_RW, &tt, NULL);
  644. } else {
  645. NV_ERROR(dev, "Invalid GART type %d\n", dev_priv->gart_info.type);
  646. ret = -EINVAL;
  647. }
  648. if (ret) {
  649. NV_ERROR(dev, "Error creating TT ctxdma: %d\n", ret);
  650. return ret;
  651. }
  652. ret = nouveau_ramht_insert(chan, tt_h, tt);
  653. nouveau_gpuobj_ref(NULL, &tt);
  654. if (ret) {
  655. NV_ERROR(dev, "Error adding TT ctxdma to RAMHT: %d\n", ret);
  656. return ret;
  657. }
  658. return 0;
  659. }
  660. void
  661. nouveau_gpuobj_channel_takedown(struct nouveau_channel *chan)
  662. {
  663. struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
  664. struct drm_device *dev = chan->dev;
  665. int i;
  666. NV_DEBUG(dev, "ch%d\n", chan->id);
  667. if (!chan->ramht)
  668. return;
  669. nouveau_ramht_ref(NULL, &chan->ramht, chan);
  670. nouveau_gpuobj_ref(NULL, &chan->vm_pd);
  671. nouveau_gpuobj_ref(NULL, &chan->vm_gart_pt);
  672. for (i = 0; i < dev_priv->vm_vram_pt_nr; i++)
  673. nouveau_gpuobj_ref(NULL, &chan->vm_vram_pt[i]);
  674. if (chan->ramin_heap.free_stack.next)
  675. drm_mm_takedown(&chan->ramin_heap);
  676. nouveau_gpuobj_ref(NULL, &chan->ramin);
  677. }
  678. int
  679. nouveau_gpuobj_suspend(struct drm_device *dev)
  680. {
  681. struct drm_nouveau_private *dev_priv = dev->dev_private;
  682. struct nouveau_gpuobj *gpuobj;
  683. int i;
  684. if (dev_priv->card_type < NV_50) {
  685. dev_priv->susres.ramin_copy = vmalloc(dev_priv->ramin_rsvd_vram);
  686. if (!dev_priv->susres.ramin_copy)
  687. return -ENOMEM;
  688. for (i = 0; i < dev_priv->ramin_rsvd_vram; i += 4)
  689. dev_priv->susres.ramin_copy[i/4] = nv_ri32(dev, i);
  690. return 0;
  691. }
  692. list_for_each_entry(gpuobj, &dev_priv->gpuobj_list, list) {
  693. if (!gpuobj->im_backing)
  694. continue;
  695. gpuobj->im_backing_suspend = vmalloc(gpuobj->size);
  696. if (!gpuobj->im_backing_suspend) {
  697. nouveau_gpuobj_resume(dev);
  698. return -ENOMEM;
  699. }
  700. for (i = 0; i < gpuobj->size; i += 4)
  701. gpuobj->im_backing_suspend[i/4] = nv_ro32(gpuobj, i);
  702. }
  703. return 0;
  704. }
  705. void
  706. nouveau_gpuobj_suspend_cleanup(struct drm_device *dev)
  707. {
  708. struct drm_nouveau_private *dev_priv = dev->dev_private;
  709. struct nouveau_gpuobj *gpuobj;
  710. if (dev_priv->card_type < NV_50) {
  711. vfree(dev_priv->susres.ramin_copy);
  712. dev_priv->susres.ramin_copy = NULL;
  713. return;
  714. }
  715. list_for_each_entry(gpuobj, &dev_priv->gpuobj_list, list) {
  716. if (!gpuobj->im_backing_suspend)
  717. continue;
  718. vfree(gpuobj->im_backing_suspend);
  719. gpuobj->im_backing_suspend = NULL;
  720. }
  721. }
  722. void
  723. nouveau_gpuobj_resume(struct drm_device *dev)
  724. {
  725. struct drm_nouveau_private *dev_priv = dev->dev_private;
  726. struct nouveau_gpuobj *gpuobj;
  727. int i;
  728. if (dev_priv->card_type < NV_50) {
  729. for (i = 0; i < dev_priv->ramin_rsvd_vram; i += 4)
  730. nv_wi32(dev, i, dev_priv->susres.ramin_copy[i/4]);
  731. nouveau_gpuobj_suspend_cleanup(dev);
  732. return;
  733. }
  734. list_for_each_entry(gpuobj, &dev_priv->gpuobj_list, list) {
  735. if (!gpuobj->im_backing_suspend)
  736. continue;
  737. for (i = 0; i < gpuobj->size; i += 4)
  738. nv_wo32(gpuobj, i, gpuobj->im_backing_suspend[i/4]);
  739. dev_priv->engine.instmem.flush(dev);
  740. }
  741. nouveau_gpuobj_suspend_cleanup(dev);
  742. }
  743. int nouveau_ioctl_grobj_alloc(struct drm_device *dev, void *data,
  744. struct drm_file *file_priv)
  745. {
  746. struct drm_nouveau_private *dev_priv = dev->dev_private;
  747. struct drm_nouveau_grobj_alloc *init = data;
  748. struct nouveau_pgraph_engine *pgraph = &dev_priv->engine.graph;
  749. struct nouveau_pgraph_object_class *grc;
  750. struct nouveau_gpuobj *gr = NULL;
  751. struct nouveau_channel *chan;
  752. int ret;
  753. NOUVEAU_GET_USER_CHANNEL_WITH_RETURN(init->channel, file_priv, chan);
  754. if (init->handle == ~0)
  755. return -EINVAL;
  756. grc = pgraph->grclass;
  757. while (grc->id) {
  758. if (grc->id == init->class)
  759. break;
  760. grc++;
  761. }
  762. if (!grc->id) {
  763. NV_ERROR(dev, "Illegal object class: 0x%x\n", init->class);
  764. return -EPERM;
  765. }
  766. if (nouveau_ramht_find(chan, init->handle))
  767. return -EEXIST;
  768. if (!grc->software)
  769. ret = nouveau_gpuobj_gr_new(chan, grc->id, &gr);
  770. else
  771. ret = nouveau_gpuobj_sw_new(chan, grc->id, &gr);
  772. if (ret) {
  773. NV_ERROR(dev, "Error creating object: %d (%d/0x%08x)\n",
  774. ret, init->channel, init->handle);
  775. return ret;
  776. }
  777. ret = nouveau_ramht_insert(chan, init->handle, gr);
  778. nouveau_gpuobj_ref(NULL, &gr);
  779. if (ret) {
  780. NV_ERROR(dev, "Error referencing object: %d (%d/0x%08x)\n",
  781. ret, init->channel, init->handle);
  782. return ret;
  783. }
  784. return 0;
  785. }
  786. int nouveau_ioctl_gpuobj_free(struct drm_device *dev, void *data,
  787. struct drm_file *file_priv)
  788. {
  789. struct drm_nouveau_gpuobj_free *objfree = data;
  790. struct nouveau_gpuobj *gpuobj;
  791. struct nouveau_channel *chan;
  792. NOUVEAU_GET_USER_CHANNEL_WITH_RETURN(objfree->channel, file_priv, chan);
  793. gpuobj = nouveau_ramht_find(chan, objfree->handle);
  794. if (!gpuobj)
  795. return -ENOENT;
  796. nouveau_ramht_remove(chan, objfree->handle);
  797. return 0;
  798. }
  799. u32
  800. nv_ro32(struct nouveau_gpuobj *gpuobj, u32 offset)
  801. {
  802. struct drm_nouveau_private *dev_priv = gpuobj->dev->dev_private;
  803. struct drm_device *dev = gpuobj->dev;
  804. if (gpuobj->pinst == ~0 || !dev_priv->ramin_available) {
  805. u64 ptr = gpuobj->vinst + offset;
  806. u32 base = ptr >> 16;
  807. u32 val;
  808. spin_lock(&dev_priv->ramin_lock);
  809. if (dev_priv->ramin_base != base) {
  810. dev_priv->ramin_base = base;
  811. nv_wr32(dev, 0x001700, dev_priv->ramin_base);
  812. }
  813. val = nv_rd32(dev, 0x700000 + (ptr & 0xffff));
  814. spin_unlock(&dev_priv->ramin_lock);
  815. return val;
  816. }
  817. return nv_ri32(dev, gpuobj->pinst + offset);
  818. }
  819. void
  820. nv_wo32(struct nouveau_gpuobj *gpuobj, u32 offset, u32 val)
  821. {
  822. struct drm_nouveau_private *dev_priv = gpuobj->dev->dev_private;
  823. struct drm_device *dev = gpuobj->dev;
  824. if (gpuobj->pinst == ~0 || !dev_priv->ramin_available) {
  825. u64 ptr = gpuobj->vinst + offset;
  826. u32 base = ptr >> 16;
  827. spin_lock(&dev_priv->ramin_lock);
  828. if (dev_priv->ramin_base != base) {
  829. dev_priv->ramin_base = base;
  830. nv_wr32(dev, 0x001700, dev_priv->ramin_base);
  831. }
  832. nv_wr32(dev, 0x700000 + (ptr & 0xffff), val);
  833. spin_unlock(&dev_priv->ramin_lock);
  834. return;
  835. }
  836. nv_wi32(dev, gpuobj->pinst + offset, val);
  837. }