nouveau_object.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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. #include "nouveau_vm.h"
  37. #include "nv50_display.h"
  38. struct nouveau_gpuobj_method {
  39. struct list_head head;
  40. u32 mthd;
  41. int (*exec)(struct nouveau_channel *, u32 class, u32 mthd, u32 data);
  42. };
  43. struct nouveau_gpuobj_class {
  44. struct list_head head;
  45. struct list_head methods;
  46. u32 id;
  47. u32 engine;
  48. };
  49. int
  50. nouveau_gpuobj_class_new(struct drm_device *dev, u32 class, u32 engine)
  51. {
  52. struct drm_nouveau_private *dev_priv = dev->dev_private;
  53. struct nouveau_gpuobj_class *oc;
  54. oc = kzalloc(sizeof(*oc), GFP_KERNEL);
  55. if (!oc)
  56. return -ENOMEM;
  57. INIT_LIST_HEAD(&oc->methods);
  58. oc->id = class;
  59. oc->engine = engine;
  60. list_add(&oc->head, &dev_priv->classes);
  61. return 0;
  62. }
  63. int
  64. nouveau_gpuobj_mthd_new(struct drm_device *dev, u32 class, u32 mthd,
  65. int (*exec)(struct nouveau_channel *, u32, u32, u32))
  66. {
  67. struct drm_nouveau_private *dev_priv = dev->dev_private;
  68. struct nouveau_gpuobj_method *om;
  69. struct nouveau_gpuobj_class *oc;
  70. list_for_each_entry(oc, &dev_priv->classes, head) {
  71. if (oc->id == class)
  72. goto found;
  73. }
  74. return -EINVAL;
  75. found:
  76. om = kzalloc(sizeof(*om), GFP_KERNEL);
  77. if (!om)
  78. return -ENOMEM;
  79. om->mthd = mthd;
  80. om->exec = exec;
  81. list_add(&om->head, &oc->methods);
  82. return 0;
  83. }
  84. int
  85. nouveau_gpuobj_mthd_call(struct nouveau_channel *chan,
  86. u32 class, u32 mthd, u32 data)
  87. {
  88. struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
  89. struct nouveau_gpuobj_method *om;
  90. struct nouveau_gpuobj_class *oc;
  91. list_for_each_entry(oc, &dev_priv->classes, head) {
  92. if (oc->id != class)
  93. continue;
  94. list_for_each_entry(om, &oc->methods, head) {
  95. if (om->mthd == mthd)
  96. return om->exec(chan, class, mthd, data);
  97. }
  98. }
  99. return -ENOENT;
  100. }
  101. int
  102. nouveau_gpuobj_mthd_call2(struct drm_device *dev, int chid,
  103. u32 class, u32 mthd, u32 data)
  104. {
  105. struct drm_nouveau_private *dev_priv = dev->dev_private;
  106. struct nouveau_channel *chan = NULL;
  107. unsigned long flags;
  108. int ret = -EINVAL;
  109. spin_lock_irqsave(&dev_priv->channels.lock, flags);
  110. if (chid > 0 && chid < dev_priv->engine.fifo.channels)
  111. chan = dev_priv->channels.ptr[chid];
  112. if (chan)
  113. ret = nouveau_gpuobj_mthd_call(chan, class, mthd, data);
  114. spin_unlock_irqrestore(&dev_priv->channels.lock, flags);
  115. return ret;
  116. }
  117. /* NVidia uses context objects to drive drawing operations.
  118. Context objects can be selected into 8 subchannels in the FIFO,
  119. and then used via DMA command buffers.
  120. A context object is referenced by a user defined handle (CARD32). The HW
  121. looks up graphics objects in a hash table in the instance RAM.
  122. An entry in the hash table consists of 2 CARD32. The first CARD32 contains
  123. the handle, the second one a bitfield, that contains the address of the
  124. object in instance RAM.
  125. The format of the second CARD32 seems to be:
  126. NV4 to NV30:
  127. 15: 0 instance_addr >> 4
  128. 17:16 engine (here uses 1 = graphics)
  129. 28:24 channel id (here uses 0)
  130. 31 valid (use 1)
  131. NV40:
  132. 15: 0 instance_addr >> 4 (maybe 19-0)
  133. 21:20 engine (here uses 1 = graphics)
  134. I'm unsure about the other bits, but using 0 seems to work.
  135. The key into the hash table depends on the object handle and channel id and
  136. is given as:
  137. */
  138. int
  139. nouveau_gpuobj_new(struct drm_device *dev, struct nouveau_channel *chan,
  140. uint32_t size, int align, uint32_t flags,
  141. struct nouveau_gpuobj **gpuobj_ret)
  142. {
  143. struct drm_nouveau_private *dev_priv = dev->dev_private;
  144. struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
  145. struct nouveau_gpuobj *gpuobj;
  146. struct drm_mm_node *ramin = NULL;
  147. int ret, i;
  148. NV_DEBUG(dev, "ch%d size=%u align=%d flags=0x%08x\n",
  149. chan ? chan->id : -1, size, align, flags);
  150. gpuobj = kzalloc(sizeof(*gpuobj), GFP_KERNEL);
  151. if (!gpuobj)
  152. return -ENOMEM;
  153. NV_DEBUG(dev, "gpuobj %p\n", gpuobj);
  154. gpuobj->dev = dev;
  155. gpuobj->flags = flags;
  156. kref_init(&gpuobj->refcount);
  157. gpuobj->size = size;
  158. spin_lock(&dev_priv->ramin_lock);
  159. list_add_tail(&gpuobj->list, &dev_priv->gpuobj_list);
  160. spin_unlock(&dev_priv->ramin_lock);
  161. if (chan) {
  162. ramin = drm_mm_search_free(&chan->ramin_heap, size, align, 0);
  163. if (ramin)
  164. ramin = drm_mm_get_block(ramin, size, align);
  165. if (!ramin) {
  166. nouveau_gpuobj_ref(NULL, &gpuobj);
  167. return -ENOMEM;
  168. }
  169. gpuobj->pinst = chan->ramin->pinst;
  170. if (gpuobj->pinst != ~0)
  171. gpuobj->pinst += ramin->start;
  172. gpuobj->cinst = ramin->start;
  173. gpuobj->vinst = ramin->start + chan->ramin->vinst;
  174. gpuobj->node = ramin;
  175. } else {
  176. ret = instmem->get(gpuobj, size, align);
  177. if (ret) {
  178. nouveau_gpuobj_ref(NULL, &gpuobj);
  179. return ret;
  180. }
  181. ret = -ENOSYS;
  182. if (!(flags & NVOBJ_FLAG_DONT_MAP))
  183. ret = instmem->map(gpuobj);
  184. if (ret)
  185. gpuobj->pinst = ~0;
  186. gpuobj->cinst = NVOBJ_CINST_GLOBAL;
  187. }
  188. if (gpuobj->flags & NVOBJ_FLAG_ZERO_ALLOC) {
  189. for (i = 0; i < gpuobj->size; i += 4)
  190. nv_wo32(gpuobj, i, 0);
  191. instmem->flush(dev);
  192. }
  193. *gpuobj_ret = gpuobj;
  194. return 0;
  195. }
  196. int
  197. nouveau_gpuobj_init(struct drm_device *dev)
  198. {
  199. struct drm_nouveau_private *dev_priv = dev->dev_private;
  200. NV_DEBUG(dev, "\n");
  201. INIT_LIST_HEAD(&dev_priv->gpuobj_list);
  202. INIT_LIST_HEAD(&dev_priv->classes);
  203. spin_lock_init(&dev_priv->ramin_lock);
  204. dev_priv->ramin_base = ~0;
  205. return 0;
  206. }
  207. void
  208. nouveau_gpuobj_takedown(struct drm_device *dev)
  209. {
  210. struct drm_nouveau_private *dev_priv = dev->dev_private;
  211. struct nouveau_gpuobj_method *om, *tm;
  212. struct nouveau_gpuobj_class *oc, *tc;
  213. NV_DEBUG(dev, "\n");
  214. list_for_each_entry_safe(oc, tc, &dev_priv->classes, head) {
  215. list_for_each_entry_safe(om, tm, &oc->methods, head) {
  216. list_del(&om->head);
  217. kfree(om);
  218. }
  219. list_del(&oc->head);
  220. kfree(oc);
  221. }
  222. BUG_ON(!list_empty(&dev_priv->gpuobj_list));
  223. }
  224. static void
  225. nouveau_gpuobj_del(struct kref *ref)
  226. {
  227. struct nouveau_gpuobj *gpuobj =
  228. container_of(ref, struct nouveau_gpuobj, refcount);
  229. struct drm_device *dev = gpuobj->dev;
  230. struct drm_nouveau_private *dev_priv = dev->dev_private;
  231. struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
  232. int i;
  233. NV_DEBUG(dev, "gpuobj %p\n", gpuobj);
  234. if (gpuobj->node && (gpuobj->flags & NVOBJ_FLAG_ZERO_FREE)) {
  235. for (i = 0; i < gpuobj->size; i += 4)
  236. nv_wo32(gpuobj, i, 0);
  237. instmem->flush(dev);
  238. }
  239. if (gpuobj->dtor)
  240. gpuobj->dtor(dev, gpuobj);
  241. if (gpuobj->cinst == NVOBJ_CINST_GLOBAL) {
  242. if (gpuobj->node) {
  243. instmem->unmap(gpuobj);
  244. instmem->put(gpuobj);
  245. }
  246. } else {
  247. if (gpuobj->node) {
  248. spin_lock(&dev_priv->ramin_lock);
  249. drm_mm_put_block(gpuobj->node);
  250. spin_unlock(&dev_priv->ramin_lock);
  251. }
  252. }
  253. spin_lock(&dev_priv->ramin_lock);
  254. list_del(&gpuobj->list);
  255. spin_unlock(&dev_priv->ramin_lock);
  256. kfree(gpuobj);
  257. }
  258. void
  259. nouveau_gpuobj_ref(struct nouveau_gpuobj *ref, struct nouveau_gpuobj **ptr)
  260. {
  261. if (ref)
  262. kref_get(&ref->refcount);
  263. if (*ptr)
  264. kref_put(&(*ptr)->refcount, nouveau_gpuobj_del);
  265. *ptr = ref;
  266. }
  267. int
  268. nouveau_gpuobj_new_fake(struct drm_device *dev, u32 pinst, u64 vinst,
  269. u32 size, u32 flags, struct nouveau_gpuobj **pgpuobj)
  270. {
  271. struct drm_nouveau_private *dev_priv = dev->dev_private;
  272. struct nouveau_gpuobj *gpuobj = NULL;
  273. int i;
  274. NV_DEBUG(dev,
  275. "pinst=0x%08x vinst=0x%010llx size=0x%08x flags=0x%08x\n",
  276. pinst, vinst, size, flags);
  277. gpuobj = kzalloc(sizeof(*gpuobj), GFP_KERNEL);
  278. if (!gpuobj)
  279. return -ENOMEM;
  280. NV_DEBUG(dev, "gpuobj %p\n", gpuobj);
  281. gpuobj->dev = dev;
  282. gpuobj->flags = flags;
  283. kref_init(&gpuobj->refcount);
  284. gpuobj->size = size;
  285. gpuobj->pinst = pinst;
  286. gpuobj->cinst = NVOBJ_CINST_GLOBAL;
  287. gpuobj->vinst = vinst;
  288. if (gpuobj->flags & NVOBJ_FLAG_ZERO_ALLOC) {
  289. for (i = 0; i < gpuobj->size; i += 4)
  290. nv_wo32(gpuobj, i, 0);
  291. dev_priv->engine.instmem.flush(dev);
  292. }
  293. spin_lock(&dev_priv->ramin_lock);
  294. list_add_tail(&gpuobj->list, &dev_priv->gpuobj_list);
  295. spin_unlock(&dev_priv->ramin_lock);
  296. *pgpuobj = gpuobj;
  297. return 0;
  298. }
  299. /*
  300. DMA objects are used to reference a piece of memory in the
  301. framebuffer, PCI or AGP address space. Each object is 16 bytes big
  302. and looks as follows:
  303. entry[0]
  304. 11:0 class (seems like I can always use 0 here)
  305. 12 page table present?
  306. 13 page entry linear?
  307. 15:14 access: 0 rw, 1 ro, 2 wo
  308. 17:16 target: 0 NV memory, 1 NV memory tiled, 2 PCI, 3 AGP
  309. 31:20 dma adjust (bits 0-11 of the address)
  310. entry[1]
  311. dma limit (size of transfer)
  312. entry[X]
  313. 1 0 readonly, 1 readwrite
  314. 31:12 dma frame address of the page (bits 12-31 of the address)
  315. entry[N]
  316. page table terminator, same value as the first pte, as does nvidia
  317. rivatv uses 0xffffffff
  318. Non linear page tables need a list of frame addresses afterwards,
  319. the rivatv project has some info on this.
  320. The method below creates a DMA object in instance RAM and returns a handle
  321. to it that can be used to set up context objects.
  322. */
  323. void
  324. nv50_gpuobj_dma_init(struct nouveau_gpuobj *obj, u32 offset, int class,
  325. u64 base, u64 size, int target, int access,
  326. u32 type, u32 comp)
  327. {
  328. struct drm_nouveau_private *dev_priv = obj->dev->dev_private;
  329. struct nouveau_instmem_engine *pinstmem = &dev_priv->engine.instmem;
  330. u32 flags0;
  331. flags0 = (comp << 29) | (type << 22) | class;
  332. flags0 |= 0x00100000;
  333. switch (access) {
  334. case NV_MEM_ACCESS_RO: flags0 |= 0x00040000; break;
  335. case NV_MEM_ACCESS_RW:
  336. case NV_MEM_ACCESS_WO: flags0 |= 0x00080000; break;
  337. default:
  338. break;
  339. }
  340. switch (target) {
  341. case NV_MEM_TARGET_VRAM:
  342. flags0 |= 0x00010000;
  343. break;
  344. case NV_MEM_TARGET_PCI:
  345. flags0 |= 0x00020000;
  346. break;
  347. case NV_MEM_TARGET_PCI_NOSNOOP:
  348. flags0 |= 0x00030000;
  349. break;
  350. case NV_MEM_TARGET_GART:
  351. base += dev_priv->gart_info.aper_base;
  352. default:
  353. flags0 &= ~0x00100000;
  354. break;
  355. }
  356. /* convert to base + limit */
  357. size = (base + size) - 1;
  358. nv_wo32(obj, offset + 0x00, flags0);
  359. nv_wo32(obj, offset + 0x04, lower_32_bits(size));
  360. nv_wo32(obj, offset + 0x08, lower_32_bits(base));
  361. nv_wo32(obj, offset + 0x0c, upper_32_bits(size) << 24 |
  362. upper_32_bits(base));
  363. nv_wo32(obj, offset + 0x10, 0x00000000);
  364. nv_wo32(obj, offset + 0x14, 0x00000000);
  365. pinstmem->flush(obj->dev);
  366. }
  367. int
  368. nv50_gpuobj_dma_new(struct nouveau_channel *chan, int class, u64 base, u64 size,
  369. int target, int access, u32 type, u32 comp,
  370. struct nouveau_gpuobj **pobj)
  371. {
  372. struct drm_device *dev = chan->dev;
  373. int ret;
  374. ret = nouveau_gpuobj_new(dev, chan, 24, 16, NVOBJ_FLAG_ZERO_FREE, pobj);
  375. if (ret)
  376. return ret;
  377. nv50_gpuobj_dma_init(*pobj, 0, class, base, size, target,
  378. access, type, comp);
  379. return 0;
  380. }
  381. int
  382. nouveau_gpuobj_dma_new(struct nouveau_channel *chan, int class, u64 base,
  383. u64 size, int access, int target,
  384. struct nouveau_gpuobj **pobj)
  385. {
  386. struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
  387. struct drm_device *dev = chan->dev;
  388. struct nouveau_gpuobj *obj;
  389. u32 flags0, flags2;
  390. int ret;
  391. if (dev_priv->card_type >= NV_50) {
  392. u32 comp = (target == NV_MEM_TARGET_VM) ? NV_MEM_COMP_VM : 0;
  393. u32 type = (target == NV_MEM_TARGET_VM) ? NV_MEM_TYPE_VM : 0;
  394. return nv50_gpuobj_dma_new(chan, class, base, size,
  395. target, access, type, comp, pobj);
  396. }
  397. if (target == NV_MEM_TARGET_GART) {
  398. struct nouveau_gpuobj *gart = dev_priv->gart_info.sg_ctxdma;
  399. if (dev_priv->gart_info.type == NOUVEAU_GART_PDMA) {
  400. if (base == 0) {
  401. nouveau_gpuobj_ref(gart, pobj);
  402. return 0;
  403. }
  404. base = nouveau_sgdma_get_physical(dev, base);
  405. target = NV_MEM_TARGET_PCI;
  406. } else {
  407. base += dev_priv->gart_info.aper_base;
  408. if (dev_priv->gart_info.type == NOUVEAU_GART_AGP)
  409. target = NV_MEM_TARGET_PCI_NOSNOOP;
  410. else
  411. target = NV_MEM_TARGET_PCI;
  412. }
  413. }
  414. flags0 = class;
  415. flags0 |= 0x00003000; /* PT present, PT linear */
  416. flags2 = 0;
  417. switch (target) {
  418. case NV_MEM_TARGET_PCI:
  419. flags0 |= 0x00020000;
  420. break;
  421. case NV_MEM_TARGET_PCI_NOSNOOP:
  422. flags0 |= 0x00030000;
  423. break;
  424. default:
  425. break;
  426. }
  427. switch (access) {
  428. case NV_MEM_ACCESS_RO:
  429. flags0 |= 0x00004000;
  430. break;
  431. case NV_MEM_ACCESS_WO:
  432. flags0 |= 0x00008000;
  433. default:
  434. flags2 |= 0x00000002;
  435. break;
  436. }
  437. flags0 |= (base & 0x00000fff) << 20;
  438. flags2 |= (base & 0xfffff000);
  439. ret = nouveau_gpuobj_new(dev, chan, 16, 16, NVOBJ_FLAG_ZERO_FREE, &obj);
  440. if (ret)
  441. return ret;
  442. nv_wo32(obj, 0x00, flags0);
  443. nv_wo32(obj, 0x04, size - 1);
  444. nv_wo32(obj, 0x08, flags2);
  445. nv_wo32(obj, 0x0c, flags2);
  446. obj->engine = NVOBJ_ENGINE_SW;
  447. obj->class = class;
  448. *pobj = obj;
  449. return 0;
  450. }
  451. /* Context objects in the instance RAM have the following structure.
  452. * On NV40 they are 32 byte long, on NV30 and smaller 16 bytes.
  453. NV4 - NV30:
  454. entry[0]
  455. 11:0 class
  456. 12 chroma key enable
  457. 13 user clip enable
  458. 14 swizzle enable
  459. 17:15 patch config:
  460. scrcopy_and, rop_and, blend_and, scrcopy, srccopy_pre, blend_pre
  461. 18 synchronize enable
  462. 19 endian: 1 big, 0 little
  463. 21:20 dither mode
  464. 23 single step enable
  465. 24 patch status: 0 invalid, 1 valid
  466. 25 context_surface 0: 1 valid
  467. 26 context surface 1: 1 valid
  468. 27 context pattern: 1 valid
  469. 28 context rop: 1 valid
  470. 29,30 context beta, beta4
  471. entry[1]
  472. 7:0 mono format
  473. 15:8 color format
  474. 31:16 notify instance address
  475. entry[2]
  476. 15:0 dma 0 instance address
  477. 31:16 dma 1 instance address
  478. entry[3]
  479. dma method traps
  480. NV40:
  481. No idea what the exact format is. Here's what can be deducted:
  482. entry[0]:
  483. 11:0 class (maybe uses more bits here?)
  484. 17 user clip enable
  485. 21:19 patch config
  486. 25 patch status valid ?
  487. entry[1]:
  488. 15:0 DMA notifier (maybe 20:0)
  489. entry[2]:
  490. 15:0 DMA 0 instance (maybe 20:0)
  491. 24 big endian
  492. entry[3]:
  493. 15:0 DMA 1 instance (maybe 20:0)
  494. entry[4]:
  495. entry[5]:
  496. set to 0?
  497. */
  498. static int
  499. nouveau_gpuobj_sw_new(struct nouveau_channel *chan, u32 handle, u16 class)
  500. {
  501. struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
  502. struct nouveau_gpuobj *gpuobj;
  503. int ret;
  504. gpuobj = kzalloc(sizeof(*gpuobj), GFP_KERNEL);
  505. if (!gpuobj)
  506. return -ENOMEM;
  507. gpuobj->dev = chan->dev;
  508. gpuobj->engine = NVOBJ_ENGINE_SW;
  509. gpuobj->class = class;
  510. kref_init(&gpuobj->refcount);
  511. gpuobj->cinst = 0x40;
  512. spin_lock(&dev_priv->ramin_lock);
  513. list_add_tail(&gpuobj->list, &dev_priv->gpuobj_list);
  514. spin_unlock(&dev_priv->ramin_lock);
  515. ret = nouveau_ramht_insert(chan, handle, gpuobj);
  516. nouveau_gpuobj_ref(NULL, &gpuobj);
  517. return ret;
  518. }
  519. int
  520. nouveau_gpuobj_gr_new(struct nouveau_channel *chan, u32 handle, int class)
  521. {
  522. struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
  523. struct nouveau_pgraph_engine *pgraph = &dev_priv->engine.graph;
  524. struct nouveau_crypt_engine *pcrypt = &dev_priv->engine.crypt;
  525. struct drm_device *dev = chan->dev;
  526. struct nouveau_gpuobj_class *oc;
  527. int ret;
  528. NV_DEBUG(dev, "ch%d class=0x%04x\n", chan->id, class);
  529. list_for_each_entry(oc, &dev_priv->classes, head) {
  530. if (oc->id == class)
  531. goto found;
  532. }
  533. NV_ERROR(dev, "illegal object class: 0x%x\n", class);
  534. return -EINVAL;
  535. found:
  536. switch (oc->engine) {
  537. case NVOBJ_ENGINE_SW:
  538. return nouveau_gpuobj_sw_new(chan, handle, class);
  539. case NVOBJ_ENGINE_GR:
  540. if ((dev_priv->card_type >= NV_20 && !chan->ramin_grctx) ||
  541. (dev_priv->card_type < NV_20 && !chan->pgraph_ctx)) {
  542. ret = pgraph->create_context(chan);
  543. if (ret)
  544. return ret;
  545. }
  546. return pgraph->object_new(chan, handle, class);
  547. case NVOBJ_ENGINE_CRYPT:
  548. if (!chan->crypt_ctx) {
  549. ret = pcrypt->create_context(chan);
  550. if (ret)
  551. return ret;
  552. }
  553. return pcrypt->object_new(chan, handle, class);
  554. }
  555. BUG_ON(1);
  556. }
  557. static int
  558. nouveau_gpuobj_channel_init_pramin(struct nouveau_channel *chan)
  559. {
  560. struct drm_device *dev = chan->dev;
  561. struct drm_nouveau_private *dev_priv = dev->dev_private;
  562. uint32_t size;
  563. uint32_t base;
  564. int ret;
  565. NV_DEBUG(dev, "ch%d\n", chan->id);
  566. /* Base amount for object storage (4KiB enough?) */
  567. size = 0x2000;
  568. base = 0;
  569. /* PGRAPH context */
  570. size += dev_priv->engine.graph.grctx_size;
  571. if (dev_priv->card_type == NV_50) {
  572. /* Various fixed table thingos */
  573. size += 0x1400; /* mostly unknown stuff */
  574. size += 0x4000; /* vm pd */
  575. base = 0x6000;
  576. /* RAMHT, not sure about setting size yet, 32KiB to be safe */
  577. size += 0x8000;
  578. /* RAMFC */
  579. size += 0x1000;
  580. }
  581. ret = nouveau_gpuobj_new(dev, NULL, size, 0x1000, 0, &chan->ramin);
  582. if (ret) {
  583. NV_ERROR(dev, "Error allocating channel PRAMIN: %d\n", ret);
  584. return ret;
  585. }
  586. ret = drm_mm_init(&chan->ramin_heap, base, size);
  587. if (ret) {
  588. NV_ERROR(dev, "Error creating PRAMIN heap: %d\n", ret);
  589. nouveau_gpuobj_ref(NULL, &chan->ramin);
  590. return ret;
  591. }
  592. return 0;
  593. }
  594. int
  595. nouveau_gpuobj_channel_init(struct nouveau_channel *chan,
  596. uint32_t vram_h, uint32_t tt_h)
  597. {
  598. struct drm_device *dev = chan->dev;
  599. struct drm_nouveau_private *dev_priv = dev->dev_private;
  600. struct nouveau_gpuobj *vram = NULL, *tt = NULL;
  601. int ret, i;
  602. NV_DEBUG(dev, "ch%d vram=0x%08x tt=0x%08x\n", chan->id, vram_h, tt_h);
  603. if (dev_priv->card_type == NV_C0) {
  604. struct nouveau_vm *vm = dev_priv->chan_vm;
  605. struct nouveau_vm_pgd *vpgd;
  606. ret = nouveau_gpuobj_new(dev, NULL, 4096, 0x1000, 0,
  607. &chan->ramin);
  608. if (ret)
  609. return ret;
  610. nouveau_vm_ref(vm, &chan->vm, NULL);
  611. vpgd = list_first_entry(&vm->pgd_list, struct nouveau_vm_pgd, head);
  612. nv_wo32(chan->ramin, 0x0200, lower_32_bits(vpgd->obj->vinst));
  613. nv_wo32(chan->ramin, 0x0204, upper_32_bits(vpgd->obj->vinst));
  614. nv_wo32(chan->ramin, 0x0208, 0xffffffff);
  615. nv_wo32(chan->ramin, 0x020c, 0x000000ff);
  616. return 0;
  617. }
  618. /* Allocate a chunk of memory for per-channel object storage */
  619. ret = nouveau_gpuobj_channel_init_pramin(chan);
  620. if (ret) {
  621. NV_ERROR(dev, "init pramin\n");
  622. return ret;
  623. }
  624. /* NV50 VM
  625. * - Allocate per-channel page-directory
  626. * - Link with shared channel VM
  627. */
  628. if (dev_priv->chan_vm) {
  629. u32 pgd_offs = (dev_priv->chipset == 0x50) ? 0x1400 : 0x0200;
  630. u64 vm_vinst = chan->ramin->vinst + pgd_offs;
  631. u32 vm_pinst = chan->ramin->pinst;
  632. if (vm_pinst != ~0)
  633. vm_pinst += pgd_offs;
  634. ret = nouveau_gpuobj_new_fake(dev, vm_pinst, vm_vinst, 0x4000,
  635. 0, &chan->vm_pd);
  636. if (ret)
  637. return ret;
  638. nouveau_vm_ref(dev_priv->chan_vm, &chan->vm, chan->vm_pd);
  639. }
  640. /* RAMHT */
  641. if (dev_priv->card_type < NV_50) {
  642. nouveau_ramht_ref(dev_priv->ramht, &chan->ramht, NULL);
  643. } else {
  644. struct nouveau_gpuobj *ramht = NULL;
  645. ret = nouveau_gpuobj_new(dev, chan, 0x8000, 16,
  646. NVOBJ_FLAG_ZERO_ALLOC, &ramht);
  647. if (ret)
  648. return ret;
  649. ret = nouveau_ramht_new(dev, ramht, &chan->ramht);
  650. nouveau_gpuobj_ref(NULL, &ramht);
  651. if (ret)
  652. return ret;
  653. /* dma objects for display sync channel semaphore blocks */
  654. for (i = 0; i < 2; i++) {
  655. struct nouveau_gpuobj *sem = NULL;
  656. struct nv50_display_crtc *dispc =
  657. &nv50_display(dev)->crtc[i];
  658. u64 offset = dispc->sem.bo->bo.mem.start << PAGE_SHIFT;
  659. ret = nouveau_gpuobj_dma_new(chan, 0x3d, offset, 0xfff,
  660. NV_MEM_ACCESS_RW,
  661. NV_MEM_TARGET_VRAM, &sem);
  662. if (ret)
  663. return ret;
  664. ret = nouveau_ramht_insert(chan, NvEvoSema0 + i, sem);
  665. nouveau_gpuobj_ref(NULL, &sem);
  666. if (ret)
  667. return ret;
  668. }
  669. }
  670. /* VRAM ctxdma */
  671. if (dev_priv->card_type >= NV_50) {
  672. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
  673. 0, (1ULL << 40), NV_MEM_ACCESS_RW,
  674. NV_MEM_TARGET_VM, &vram);
  675. if (ret) {
  676. NV_ERROR(dev, "Error creating VRAM ctxdma: %d\n", ret);
  677. return ret;
  678. }
  679. } else {
  680. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
  681. 0, dev_priv->fb_available_size,
  682. NV_MEM_ACCESS_RW,
  683. NV_MEM_TARGET_VRAM, &vram);
  684. if (ret) {
  685. NV_ERROR(dev, "Error creating VRAM ctxdma: %d\n", ret);
  686. return ret;
  687. }
  688. }
  689. ret = nouveau_ramht_insert(chan, vram_h, vram);
  690. nouveau_gpuobj_ref(NULL, &vram);
  691. if (ret) {
  692. NV_ERROR(dev, "Error adding VRAM ctxdma to RAMHT: %d\n", ret);
  693. return ret;
  694. }
  695. /* TT memory ctxdma */
  696. if (dev_priv->card_type >= NV_50) {
  697. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
  698. 0, (1ULL << 40), NV_MEM_ACCESS_RW,
  699. NV_MEM_TARGET_VM, &tt);
  700. } else {
  701. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
  702. 0, dev_priv->gart_info.aper_size,
  703. NV_MEM_ACCESS_RW,
  704. NV_MEM_TARGET_GART, &tt);
  705. }
  706. if (ret) {
  707. NV_ERROR(dev, "Error creating TT ctxdma: %d\n", ret);
  708. return ret;
  709. }
  710. ret = nouveau_ramht_insert(chan, tt_h, tt);
  711. nouveau_gpuobj_ref(NULL, &tt);
  712. if (ret) {
  713. NV_ERROR(dev, "Error adding TT ctxdma to RAMHT: %d\n", ret);
  714. return ret;
  715. }
  716. return 0;
  717. }
  718. void
  719. nouveau_gpuobj_channel_takedown(struct nouveau_channel *chan)
  720. {
  721. struct drm_device *dev = chan->dev;
  722. NV_DEBUG(dev, "ch%d\n", chan->id);
  723. nouveau_ramht_ref(NULL, &chan->ramht, chan);
  724. nouveau_vm_ref(NULL, &chan->vm, chan->vm_pd);
  725. nouveau_gpuobj_ref(NULL, &chan->vm_pd);
  726. if (drm_mm_initialized(&chan->ramin_heap))
  727. drm_mm_takedown(&chan->ramin_heap);
  728. nouveau_gpuobj_ref(NULL, &chan->ramin);
  729. }
  730. int
  731. nouveau_gpuobj_suspend(struct drm_device *dev)
  732. {
  733. struct drm_nouveau_private *dev_priv = dev->dev_private;
  734. struct nouveau_gpuobj *gpuobj;
  735. int i;
  736. list_for_each_entry(gpuobj, &dev_priv->gpuobj_list, list) {
  737. if (gpuobj->cinst != NVOBJ_CINST_GLOBAL)
  738. continue;
  739. gpuobj->suspend = vmalloc(gpuobj->size);
  740. if (!gpuobj->suspend) {
  741. nouveau_gpuobj_resume(dev);
  742. return -ENOMEM;
  743. }
  744. for (i = 0; i < gpuobj->size; i += 4)
  745. gpuobj->suspend[i/4] = nv_ro32(gpuobj, i);
  746. }
  747. return 0;
  748. }
  749. void
  750. nouveau_gpuobj_resume(struct drm_device *dev)
  751. {
  752. struct drm_nouveau_private *dev_priv = dev->dev_private;
  753. struct nouveau_gpuobj *gpuobj;
  754. int i;
  755. list_for_each_entry(gpuobj, &dev_priv->gpuobj_list, list) {
  756. if (!gpuobj->suspend)
  757. continue;
  758. for (i = 0; i < gpuobj->size; i += 4)
  759. nv_wo32(gpuobj, i, gpuobj->suspend[i/4]);
  760. vfree(gpuobj->suspend);
  761. gpuobj->suspend = NULL;
  762. }
  763. dev_priv->engine.instmem.flush(dev);
  764. }
  765. int nouveau_ioctl_grobj_alloc(struct drm_device *dev, void *data,
  766. struct drm_file *file_priv)
  767. {
  768. struct drm_nouveau_grobj_alloc *init = data;
  769. struct nouveau_channel *chan;
  770. int ret;
  771. if (init->handle == ~0)
  772. return -EINVAL;
  773. chan = nouveau_channel_get(dev, file_priv, init->channel);
  774. if (IS_ERR(chan))
  775. return PTR_ERR(chan);
  776. if (nouveau_ramht_find(chan, init->handle)) {
  777. ret = -EEXIST;
  778. goto out;
  779. }
  780. ret = nouveau_gpuobj_gr_new(chan, init->handle, init->class);
  781. if (ret) {
  782. NV_ERROR(dev, "Error creating object: %d (%d/0x%08x)\n",
  783. ret, init->channel, init->handle);
  784. }
  785. out:
  786. nouveau_channel_put(&chan);
  787. return ret;
  788. }
  789. int nouveau_ioctl_gpuobj_free(struct drm_device *dev, void *data,
  790. struct drm_file *file_priv)
  791. {
  792. struct drm_nouveau_gpuobj_free *objfree = data;
  793. struct nouveau_channel *chan;
  794. int ret;
  795. chan = nouveau_channel_get(dev, file_priv, objfree->channel);
  796. if (IS_ERR(chan))
  797. return PTR_ERR(chan);
  798. /* Synchronize with the user channel */
  799. nouveau_channel_idle(chan);
  800. ret = nouveau_ramht_remove(chan, objfree->handle);
  801. nouveau_channel_put(&chan);
  802. return ret;
  803. }
  804. u32
  805. nv_ro32(struct nouveau_gpuobj *gpuobj, u32 offset)
  806. {
  807. struct drm_nouveau_private *dev_priv = gpuobj->dev->dev_private;
  808. struct drm_device *dev = gpuobj->dev;
  809. unsigned long flags;
  810. if (gpuobj->pinst == ~0 || !dev_priv->ramin_available) {
  811. u64 ptr = gpuobj->vinst + offset;
  812. u32 base = ptr >> 16;
  813. u32 val;
  814. spin_lock_irqsave(&dev_priv->vm_lock, flags);
  815. if (dev_priv->ramin_base != base) {
  816. dev_priv->ramin_base = base;
  817. nv_wr32(dev, 0x001700, dev_priv->ramin_base);
  818. }
  819. val = nv_rd32(dev, 0x700000 + (ptr & 0xffff));
  820. spin_unlock_irqrestore(&dev_priv->vm_lock, flags);
  821. return val;
  822. }
  823. return nv_ri32(dev, gpuobj->pinst + offset);
  824. }
  825. void
  826. nv_wo32(struct nouveau_gpuobj *gpuobj, u32 offset, u32 val)
  827. {
  828. struct drm_nouveau_private *dev_priv = gpuobj->dev->dev_private;
  829. struct drm_device *dev = gpuobj->dev;
  830. unsigned long flags;
  831. if (gpuobj->pinst == ~0 || !dev_priv->ramin_available) {
  832. u64 ptr = gpuobj->vinst + offset;
  833. u32 base = ptr >> 16;
  834. spin_lock_irqsave(&dev_priv->vm_lock, flags);
  835. if (dev_priv->ramin_base != base) {
  836. dev_priv->ramin_base = base;
  837. nv_wr32(dev, 0x001700, dev_priv->ramin_base);
  838. }
  839. nv_wr32(dev, 0x700000 + (ptr & 0xffff), val);
  840. spin_unlock_irqrestore(&dev_priv->vm_lock, flags);
  841. return;
  842. }
  843. nv_wi32(dev, gpuobj->pinst + offset, val);
  844. }