nouveau_object.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  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. static uint32_t
  300. nouveau_gpuobj_class_instmem_size(struct drm_device *dev, int class)
  301. {
  302. struct drm_nouveau_private *dev_priv = dev->dev_private;
  303. /*XXX: dodgy hack for now */
  304. if (dev_priv->card_type >= NV_50)
  305. return 24;
  306. if (dev_priv->card_type >= NV_40)
  307. return 32;
  308. return 16;
  309. }
  310. /*
  311. DMA objects are used to reference a piece of memory in the
  312. framebuffer, PCI or AGP address space. Each object is 16 bytes big
  313. and looks as follows:
  314. entry[0]
  315. 11:0 class (seems like I can always use 0 here)
  316. 12 page table present?
  317. 13 page entry linear?
  318. 15:14 access: 0 rw, 1 ro, 2 wo
  319. 17:16 target: 0 NV memory, 1 NV memory tiled, 2 PCI, 3 AGP
  320. 31:20 dma adjust (bits 0-11 of the address)
  321. entry[1]
  322. dma limit (size of transfer)
  323. entry[X]
  324. 1 0 readonly, 1 readwrite
  325. 31:12 dma frame address of the page (bits 12-31 of the address)
  326. entry[N]
  327. page table terminator, same value as the first pte, as does nvidia
  328. rivatv uses 0xffffffff
  329. Non linear page tables need a list of frame addresses afterwards,
  330. the rivatv project has some info on this.
  331. The method below creates a DMA object in instance RAM and returns a handle
  332. to it that can be used to set up context objects.
  333. */
  334. void
  335. nv50_gpuobj_dma_init(struct nouveau_gpuobj *obj, u32 offset, int class,
  336. u64 base, u64 size, int target, int access,
  337. u32 type, u32 comp)
  338. {
  339. struct drm_nouveau_private *dev_priv = obj->dev->dev_private;
  340. struct nouveau_instmem_engine *pinstmem = &dev_priv->engine.instmem;
  341. u32 flags0;
  342. flags0 = (comp << 29) | (type << 22) | class;
  343. flags0 |= 0x00100000;
  344. switch (access) {
  345. case NV_MEM_ACCESS_RO: flags0 |= 0x00040000; break;
  346. case NV_MEM_ACCESS_RW:
  347. case NV_MEM_ACCESS_WO: flags0 |= 0x00080000; break;
  348. default:
  349. break;
  350. }
  351. switch (target) {
  352. case NV_MEM_TARGET_VRAM:
  353. flags0 |= 0x00010000;
  354. break;
  355. case NV_MEM_TARGET_PCI:
  356. flags0 |= 0x00020000;
  357. break;
  358. case NV_MEM_TARGET_PCI_NOSNOOP:
  359. flags0 |= 0x00030000;
  360. break;
  361. case NV_MEM_TARGET_GART:
  362. base += dev_priv->gart_info.aper_base;
  363. default:
  364. flags0 &= ~0x00100000;
  365. break;
  366. }
  367. /* convert to base + limit */
  368. size = (base + size) - 1;
  369. nv_wo32(obj, offset + 0x00, flags0);
  370. nv_wo32(obj, offset + 0x04, lower_32_bits(size));
  371. nv_wo32(obj, offset + 0x08, lower_32_bits(base));
  372. nv_wo32(obj, offset + 0x0c, upper_32_bits(size) << 24 |
  373. upper_32_bits(base));
  374. nv_wo32(obj, offset + 0x10, 0x00000000);
  375. nv_wo32(obj, offset + 0x14, 0x00000000);
  376. pinstmem->flush(obj->dev);
  377. }
  378. int
  379. nv50_gpuobj_dma_new(struct nouveau_channel *chan, int class, u64 base, u64 size,
  380. int target, int access, u32 type, u32 comp,
  381. struct nouveau_gpuobj **pobj)
  382. {
  383. struct drm_device *dev = chan->dev;
  384. int ret;
  385. ret = nouveau_gpuobj_new(dev, chan, 24, 16, NVOBJ_FLAG_ZERO_FREE, pobj);
  386. if (ret)
  387. return ret;
  388. nv50_gpuobj_dma_init(*pobj, 0, class, base, size, target,
  389. access, type, comp);
  390. return 0;
  391. }
  392. int
  393. nouveau_gpuobj_dma_new(struct nouveau_channel *chan, int class, u64 base,
  394. u64 size, int access, int target,
  395. struct nouveau_gpuobj **pobj)
  396. {
  397. struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
  398. struct drm_device *dev = chan->dev;
  399. struct nouveau_gpuobj *obj;
  400. u32 flags0, flags2;
  401. int ret;
  402. if (dev_priv->card_type >= NV_50) {
  403. u32 comp = (target == NV_MEM_TARGET_VM) ? NV_MEM_COMP_VM : 0;
  404. u32 type = (target == NV_MEM_TARGET_VM) ? NV_MEM_TYPE_VM : 0;
  405. return nv50_gpuobj_dma_new(chan, class, base, size,
  406. target, access, type, comp, pobj);
  407. }
  408. if (target == NV_MEM_TARGET_GART) {
  409. struct nouveau_gpuobj *gart = dev_priv->gart_info.sg_ctxdma;
  410. if (dev_priv->gart_info.type == NOUVEAU_GART_PDMA) {
  411. if (base == 0) {
  412. nouveau_gpuobj_ref(gart, pobj);
  413. return 0;
  414. }
  415. base = nouveau_sgdma_get_physical(dev, base);
  416. target = NV_MEM_TARGET_PCI;
  417. } else {
  418. base += dev_priv->gart_info.aper_base;
  419. if (dev_priv->gart_info.type == NOUVEAU_GART_AGP)
  420. target = NV_MEM_TARGET_PCI_NOSNOOP;
  421. else
  422. target = NV_MEM_TARGET_PCI;
  423. }
  424. }
  425. flags0 = class;
  426. flags0 |= 0x00003000; /* PT present, PT linear */
  427. flags2 = 0;
  428. switch (target) {
  429. case NV_MEM_TARGET_PCI:
  430. flags0 |= 0x00020000;
  431. break;
  432. case NV_MEM_TARGET_PCI_NOSNOOP:
  433. flags0 |= 0x00030000;
  434. break;
  435. default:
  436. break;
  437. }
  438. switch (access) {
  439. case NV_MEM_ACCESS_RO:
  440. flags0 |= 0x00004000;
  441. break;
  442. case NV_MEM_ACCESS_WO:
  443. flags0 |= 0x00008000;
  444. default:
  445. flags2 |= 0x00000002;
  446. break;
  447. }
  448. flags0 |= (base & 0x00000fff) << 20;
  449. flags2 |= (base & 0xfffff000);
  450. ret = nouveau_gpuobj_new(dev, chan, 16, 16, NVOBJ_FLAG_ZERO_FREE, &obj);
  451. if (ret)
  452. return ret;
  453. nv_wo32(obj, 0x00, flags0);
  454. nv_wo32(obj, 0x04, size - 1);
  455. nv_wo32(obj, 0x08, flags2);
  456. nv_wo32(obj, 0x0c, flags2);
  457. obj->engine = NVOBJ_ENGINE_SW;
  458. obj->class = class;
  459. *pobj = obj;
  460. return 0;
  461. }
  462. /* Context objects in the instance RAM have the following structure.
  463. * On NV40 they are 32 byte long, on NV30 and smaller 16 bytes.
  464. NV4 - NV30:
  465. entry[0]
  466. 11:0 class
  467. 12 chroma key enable
  468. 13 user clip enable
  469. 14 swizzle enable
  470. 17:15 patch config:
  471. scrcopy_and, rop_and, blend_and, scrcopy, srccopy_pre, blend_pre
  472. 18 synchronize enable
  473. 19 endian: 1 big, 0 little
  474. 21:20 dither mode
  475. 23 single step enable
  476. 24 patch status: 0 invalid, 1 valid
  477. 25 context_surface 0: 1 valid
  478. 26 context surface 1: 1 valid
  479. 27 context pattern: 1 valid
  480. 28 context rop: 1 valid
  481. 29,30 context beta, beta4
  482. entry[1]
  483. 7:0 mono format
  484. 15:8 color format
  485. 31:16 notify instance address
  486. entry[2]
  487. 15:0 dma 0 instance address
  488. 31:16 dma 1 instance address
  489. entry[3]
  490. dma method traps
  491. NV40:
  492. No idea what the exact format is. Here's what can be deducted:
  493. entry[0]:
  494. 11:0 class (maybe uses more bits here?)
  495. 17 user clip enable
  496. 21:19 patch config
  497. 25 patch status valid ?
  498. entry[1]:
  499. 15:0 DMA notifier (maybe 20:0)
  500. entry[2]:
  501. 15:0 DMA 0 instance (maybe 20:0)
  502. 24 big endian
  503. entry[3]:
  504. 15:0 DMA 1 instance (maybe 20:0)
  505. entry[4]:
  506. entry[5]:
  507. set to 0?
  508. */
  509. static int
  510. nouveau_gpuobj_sw_new(struct nouveau_channel *chan, int class,
  511. struct nouveau_gpuobj **gpuobj_ret)
  512. {
  513. struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
  514. struct nouveau_gpuobj *gpuobj;
  515. gpuobj = kzalloc(sizeof(*gpuobj), GFP_KERNEL);
  516. if (!gpuobj)
  517. return -ENOMEM;
  518. gpuobj->dev = chan->dev;
  519. gpuobj->engine = NVOBJ_ENGINE_SW;
  520. gpuobj->class = class;
  521. kref_init(&gpuobj->refcount);
  522. gpuobj->cinst = 0x40;
  523. spin_lock(&dev_priv->ramin_lock);
  524. list_add_tail(&gpuobj->list, &dev_priv->gpuobj_list);
  525. spin_unlock(&dev_priv->ramin_lock);
  526. *gpuobj_ret = gpuobj;
  527. return 0;
  528. }
  529. int
  530. nouveau_gpuobj_gr_new(struct nouveau_channel *chan, u32 handle, int class)
  531. {
  532. struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
  533. struct drm_device *dev = chan->dev;
  534. struct nouveau_gpuobj_class *oc;
  535. struct nouveau_gpuobj *gpuobj;
  536. int ret;
  537. NV_DEBUG(dev, "ch%d class=0x%04x\n", chan->id, class);
  538. list_for_each_entry(oc, &dev_priv->classes, head) {
  539. if (oc->id == class)
  540. goto found;
  541. }
  542. NV_ERROR(dev, "illegal object class: 0x%x\n", class);
  543. return -EINVAL;
  544. found:
  545. switch (oc->engine) {
  546. case NVOBJ_ENGINE_SW:
  547. if (dev_priv->card_type < NV_C0) {
  548. ret = nouveau_gpuobj_sw_new(chan, class, &gpuobj);
  549. if (ret)
  550. return ret;
  551. goto insert;
  552. }
  553. break;
  554. case NVOBJ_ENGINE_GR:
  555. if ((dev_priv->card_type >= NV_20 && !chan->ramin_grctx) ||
  556. (dev_priv->card_type < NV_20 && !chan->pgraph_ctx)) {
  557. struct nouveau_pgraph_engine *pgraph =
  558. &dev_priv->engine.graph;
  559. ret = pgraph->create_context(chan);
  560. if (ret)
  561. return ret;
  562. }
  563. break;
  564. case NVOBJ_ENGINE_CRYPT:
  565. if (!chan->crypt_ctx) {
  566. struct nouveau_crypt_engine *pcrypt =
  567. &dev_priv->engine.crypt;
  568. ret = pcrypt->create_context(chan);
  569. if (ret)
  570. return ret;
  571. }
  572. break;
  573. }
  574. /* we're done if this is fermi */
  575. if (dev_priv->card_type >= NV_C0)
  576. return 0;
  577. ret = nouveau_gpuobj_new(dev, chan,
  578. nouveau_gpuobj_class_instmem_size(dev, class),
  579. 16,
  580. NVOBJ_FLAG_ZERO_ALLOC | NVOBJ_FLAG_ZERO_FREE,
  581. &gpuobj);
  582. if (ret) {
  583. NV_ERROR(dev, "error creating gpuobj: %d\n", ret);
  584. return ret;
  585. }
  586. if (dev_priv->card_type >= NV_50) {
  587. nv_wo32(gpuobj, 0, class);
  588. nv_wo32(gpuobj, 20, 0x00010000);
  589. } else {
  590. switch (class) {
  591. case NV_CLASS_NULL:
  592. nv_wo32(gpuobj, 0, 0x00001030);
  593. nv_wo32(gpuobj, 4, 0xFFFFFFFF);
  594. break;
  595. default:
  596. if (dev_priv->card_type >= NV_40) {
  597. nv_wo32(gpuobj, 0, class);
  598. #ifdef __BIG_ENDIAN
  599. nv_wo32(gpuobj, 8, 0x01000000);
  600. #endif
  601. } else {
  602. #ifdef __BIG_ENDIAN
  603. nv_wo32(gpuobj, 0, class | 0x00080000);
  604. #else
  605. nv_wo32(gpuobj, 0, class);
  606. #endif
  607. }
  608. }
  609. }
  610. dev_priv->engine.instmem.flush(dev);
  611. gpuobj->engine = oc->engine;
  612. gpuobj->class = oc->id;
  613. insert:
  614. ret = nouveau_ramht_insert(chan, handle, gpuobj);
  615. if (ret)
  616. NV_ERROR(dev, "error adding gpuobj to RAMHT: %d\n", ret);
  617. nouveau_gpuobj_ref(NULL, &gpuobj);
  618. return ret;
  619. }
  620. static int
  621. nouveau_gpuobj_channel_init_pramin(struct nouveau_channel *chan)
  622. {
  623. struct drm_device *dev = chan->dev;
  624. struct drm_nouveau_private *dev_priv = dev->dev_private;
  625. uint32_t size;
  626. uint32_t base;
  627. int ret;
  628. NV_DEBUG(dev, "ch%d\n", chan->id);
  629. /* Base amount for object storage (4KiB enough?) */
  630. size = 0x2000;
  631. base = 0;
  632. /* PGRAPH context */
  633. size += dev_priv->engine.graph.grctx_size;
  634. if (dev_priv->card_type == NV_50) {
  635. /* Various fixed table thingos */
  636. size += 0x1400; /* mostly unknown stuff */
  637. size += 0x4000; /* vm pd */
  638. base = 0x6000;
  639. /* RAMHT, not sure about setting size yet, 32KiB to be safe */
  640. size += 0x8000;
  641. /* RAMFC */
  642. size += 0x1000;
  643. }
  644. ret = nouveau_gpuobj_new(dev, NULL, size, 0x1000, 0, &chan->ramin);
  645. if (ret) {
  646. NV_ERROR(dev, "Error allocating channel PRAMIN: %d\n", ret);
  647. return ret;
  648. }
  649. ret = drm_mm_init(&chan->ramin_heap, base, size);
  650. if (ret) {
  651. NV_ERROR(dev, "Error creating PRAMIN heap: %d\n", ret);
  652. nouveau_gpuobj_ref(NULL, &chan->ramin);
  653. return ret;
  654. }
  655. return 0;
  656. }
  657. int
  658. nouveau_gpuobj_channel_init(struct nouveau_channel *chan,
  659. uint32_t vram_h, uint32_t tt_h)
  660. {
  661. struct drm_device *dev = chan->dev;
  662. struct drm_nouveau_private *dev_priv = dev->dev_private;
  663. struct nouveau_gpuobj *vram = NULL, *tt = NULL;
  664. int ret, i;
  665. NV_DEBUG(dev, "ch%d vram=0x%08x tt=0x%08x\n", chan->id, vram_h, tt_h);
  666. if (dev_priv->card_type == NV_C0) {
  667. struct nouveau_vm *vm = dev_priv->chan_vm;
  668. struct nouveau_vm_pgd *vpgd;
  669. ret = nouveau_gpuobj_new(dev, NULL, 4096, 0x1000, 0,
  670. &chan->ramin);
  671. if (ret)
  672. return ret;
  673. nouveau_vm_ref(vm, &chan->vm, NULL);
  674. vpgd = list_first_entry(&vm->pgd_list, struct nouveau_vm_pgd, head);
  675. nv_wo32(chan->ramin, 0x0200, lower_32_bits(vpgd->obj->vinst));
  676. nv_wo32(chan->ramin, 0x0204, upper_32_bits(vpgd->obj->vinst));
  677. nv_wo32(chan->ramin, 0x0208, 0xffffffff);
  678. nv_wo32(chan->ramin, 0x020c, 0x000000ff);
  679. return 0;
  680. }
  681. /* Allocate a chunk of memory for per-channel object storage */
  682. ret = nouveau_gpuobj_channel_init_pramin(chan);
  683. if (ret) {
  684. NV_ERROR(dev, "init pramin\n");
  685. return ret;
  686. }
  687. /* NV50 VM
  688. * - Allocate per-channel page-directory
  689. * - Link with shared channel VM
  690. */
  691. if (dev_priv->chan_vm) {
  692. u32 pgd_offs = (dev_priv->chipset == 0x50) ? 0x1400 : 0x0200;
  693. u64 vm_vinst = chan->ramin->vinst + pgd_offs;
  694. u32 vm_pinst = chan->ramin->pinst;
  695. if (vm_pinst != ~0)
  696. vm_pinst += pgd_offs;
  697. ret = nouveau_gpuobj_new_fake(dev, vm_pinst, vm_vinst, 0x4000,
  698. 0, &chan->vm_pd);
  699. if (ret)
  700. return ret;
  701. nouveau_vm_ref(dev_priv->chan_vm, &chan->vm, chan->vm_pd);
  702. }
  703. /* RAMHT */
  704. if (dev_priv->card_type < NV_50) {
  705. nouveau_ramht_ref(dev_priv->ramht, &chan->ramht, NULL);
  706. } else {
  707. struct nouveau_gpuobj *ramht = NULL;
  708. ret = nouveau_gpuobj_new(dev, chan, 0x8000, 16,
  709. NVOBJ_FLAG_ZERO_ALLOC, &ramht);
  710. if (ret)
  711. return ret;
  712. ret = nouveau_ramht_new(dev, ramht, &chan->ramht);
  713. nouveau_gpuobj_ref(NULL, &ramht);
  714. if (ret)
  715. return ret;
  716. /* dma objects for display sync channel semaphore blocks */
  717. for (i = 0; i < 2; i++) {
  718. struct nouveau_gpuobj *sem = NULL;
  719. struct nv50_display_crtc *dispc =
  720. &nv50_display(dev)->crtc[i];
  721. u64 offset = dispc->sem.bo->bo.mem.start << PAGE_SHIFT;
  722. ret = nouveau_gpuobj_dma_new(chan, 0x3d, offset, 0xfff,
  723. NV_MEM_ACCESS_RW,
  724. NV_MEM_TARGET_VRAM, &sem);
  725. if (ret)
  726. return ret;
  727. ret = nouveau_ramht_insert(chan, NvEvoSema0 + i, sem);
  728. nouveau_gpuobj_ref(NULL, &sem);
  729. if (ret)
  730. return ret;
  731. }
  732. }
  733. /* VRAM ctxdma */
  734. if (dev_priv->card_type >= NV_50) {
  735. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
  736. 0, (1ULL << 40), NV_MEM_ACCESS_RW,
  737. NV_MEM_TARGET_VM, &vram);
  738. if (ret) {
  739. NV_ERROR(dev, "Error creating VRAM ctxdma: %d\n", ret);
  740. return ret;
  741. }
  742. } else {
  743. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
  744. 0, dev_priv->fb_available_size,
  745. NV_MEM_ACCESS_RW,
  746. NV_MEM_TARGET_VRAM, &vram);
  747. if (ret) {
  748. NV_ERROR(dev, "Error creating VRAM ctxdma: %d\n", ret);
  749. return ret;
  750. }
  751. }
  752. ret = nouveau_ramht_insert(chan, vram_h, vram);
  753. nouveau_gpuobj_ref(NULL, &vram);
  754. if (ret) {
  755. NV_ERROR(dev, "Error adding VRAM ctxdma to RAMHT: %d\n", ret);
  756. return ret;
  757. }
  758. /* TT memory ctxdma */
  759. if (dev_priv->card_type >= NV_50) {
  760. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
  761. 0, (1ULL << 40), NV_MEM_ACCESS_RW,
  762. NV_MEM_TARGET_VM, &tt);
  763. } else {
  764. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
  765. 0, dev_priv->gart_info.aper_size,
  766. NV_MEM_ACCESS_RW,
  767. NV_MEM_TARGET_GART, &tt);
  768. }
  769. if (ret) {
  770. NV_ERROR(dev, "Error creating TT ctxdma: %d\n", ret);
  771. return ret;
  772. }
  773. ret = nouveau_ramht_insert(chan, tt_h, tt);
  774. nouveau_gpuobj_ref(NULL, &tt);
  775. if (ret) {
  776. NV_ERROR(dev, "Error adding TT ctxdma to RAMHT: %d\n", ret);
  777. return ret;
  778. }
  779. return 0;
  780. }
  781. void
  782. nouveau_gpuobj_channel_takedown(struct nouveau_channel *chan)
  783. {
  784. struct drm_device *dev = chan->dev;
  785. NV_DEBUG(dev, "ch%d\n", chan->id);
  786. nouveau_ramht_ref(NULL, &chan->ramht, chan);
  787. nouveau_vm_ref(NULL, &chan->vm, chan->vm_pd);
  788. nouveau_gpuobj_ref(NULL, &chan->vm_pd);
  789. if (drm_mm_initialized(&chan->ramin_heap))
  790. drm_mm_takedown(&chan->ramin_heap);
  791. nouveau_gpuobj_ref(NULL, &chan->ramin);
  792. }
  793. int
  794. nouveau_gpuobj_suspend(struct drm_device *dev)
  795. {
  796. struct drm_nouveau_private *dev_priv = dev->dev_private;
  797. struct nouveau_gpuobj *gpuobj;
  798. int i;
  799. list_for_each_entry(gpuobj, &dev_priv->gpuobj_list, list) {
  800. if (gpuobj->cinst != NVOBJ_CINST_GLOBAL)
  801. continue;
  802. gpuobj->suspend = vmalloc(gpuobj->size);
  803. if (!gpuobj->suspend) {
  804. nouveau_gpuobj_resume(dev);
  805. return -ENOMEM;
  806. }
  807. for (i = 0; i < gpuobj->size; i += 4)
  808. gpuobj->suspend[i/4] = nv_ro32(gpuobj, i);
  809. }
  810. return 0;
  811. }
  812. void
  813. nouveau_gpuobj_resume(struct drm_device *dev)
  814. {
  815. struct drm_nouveau_private *dev_priv = dev->dev_private;
  816. struct nouveau_gpuobj *gpuobj;
  817. int i;
  818. list_for_each_entry(gpuobj, &dev_priv->gpuobj_list, list) {
  819. if (!gpuobj->suspend)
  820. continue;
  821. for (i = 0; i < gpuobj->size; i += 4)
  822. nv_wo32(gpuobj, i, gpuobj->suspend[i/4]);
  823. vfree(gpuobj->suspend);
  824. gpuobj->suspend = NULL;
  825. }
  826. dev_priv->engine.instmem.flush(dev);
  827. }
  828. int nouveau_ioctl_grobj_alloc(struct drm_device *dev, void *data,
  829. struct drm_file *file_priv)
  830. {
  831. struct drm_nouveau_grobj_alloc *init = data;
  832. struct nouveau_channel *chan;
  833. int ret;
  834. if (init->handle == ~0)
  835. return -EINVAL;
  836. chan = nouveau_channel_get(dev, file_priv, init->channel);
  837. if (IS_ERR(chan))
  838. return PTR_ERR(chan);
  839. if (nouveau_ramht_find(chan, init->handle)) {
  840. ret = -EEXIST;
  841. goto out;
  842. }
  843. ret = nouveau_gpuobj_gr_new(chan, init->handle, init->class);
  844. if (ret) {
  845. NV_ERROR(dev, "Error creating object: %d (%d/0x%08x)\n",
  846. ret, init->channel, init->handle);
  847. }
  848. out:
  849. nouveau_channel_put(&chan);
  850. return ret;
  851. }
  852. int nouveau_ioctl_gpuobj_free(struct drm_device *dev, void *data,
  853. struct drm_file *file_priv)
  854. {
  855. struct drm_nouveau_gpuobj_free *objfree = data;
  856. struct nouveau_channel *chan;
  857. int ret;
  858. chan = nouveau_channel_get(dev, file_priv, objfree->channel);
  859. if (IS_ERR(chan))
  860. return PTR_ERR(chan);
  861. /* Synchronize with the user channel */
  862. nouveau_channel_idle(chan);
  863. ret = nouveau_ramht_remove(chan, objfree->handle);
  864. nouveau_channel_put(&chan);
  865. return ret;
  866. }
  867. u32
  868. nv_ro32(struct nouveau_gpuobj *gpuobj, u32 offset)
  869. {
  870. struct drm_nouveau_private *dev_priv = gpuobj->dev->dev_private;
  871. struct drm_device *dev = gpuobj->dev;
  872. if (gpuobj->pinst == ~0 || !dev_priv->ramin_available) {
  873. u64 ptr = gpuobj->vinst + offset;
  874. u32 base = ptr >> 16;
  875. u32 val;
  876. spin_lock(&dev_priv->ramin_lock);
  877. if (dev_priv->ramin_base != base) {
  878. dev_priv->ramin_base = base;
  879. nv_wr32(dev, 0x001700, dev_priv->ramin_base);
  880. }
  881. val = nv_rd32(dev, 0x700000 + (ptr & 0xffff));
  882. spin_unlock(&dev_priv->ramin_lock);
  883. return val;
  884. }
  885. return nv_ri32(dev, gpuobj->pinst + offset);
  886. }
  887. void
  888. nv_wo32(struct nouveau_gpuobj *gpuobj, u32 offset, u32 val)
  889. {
  890. struct drm_nouveau_private *dev_priv = gpuobj->dev->dev_private;
  891. struct drm_device *dev = gpuobj->dev;
  892. if (gpuobj->pinst == ~0 || !dev_priv->ramin_available) {
  893. u64 ptr = gpuobj->vinst + offset;
  894. u32 base = ptr >> 16;
  895. spin_lock(&dev_priv->ramin_lock);
  896. if (dev_priv->ramin_base != base) {
  897. dev_priv->ramin_base = base;
  898. nv_wr32(dev, 0x001700, dev_priv->ramin_base);
  899. }
  900. nv_wr32(dev, 0x700000 + (ptr & 0xffff), val);
  901. spin_unlock(&dev_priv->ramin_lock);
  902. return;
  903. }
  904. nv_wi32(dev, gpuobj->pinst + offset, val);
  905. }