nouveau_object.c 26 KB

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