nouveau_object.c 25 KB

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