nouveau_object.c 27 KB

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