nouveau_object.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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_20 && !chan->ramin_grctx) ||
  551. (dev_priv->card_type < NV_20 && !chan->pgraph_ctx)) {
  552. struct nouveau_pgraph_engine *pgraph =
  553. &dev_priv->engine.graph;
  554. ret = pgraph->create_context(chan);
  555. if (ret)
  556. return ret;
  557. }
  558. break;
  559. case NVOBJ_ENGINE_CRYPT:
  560. if (!chan->crypt_ctx) {
  561. struct nouveau_crypt_engine *pcrypt =
  562. &dev_priv->engine.crypt;
  563. ret = pcrypt->create_context(chan);
  564. if (ret)
  565. return ret;
  566. }
  567. break;
  568. }
  569. /* we're done if this is fermi */
  570. if (dev_priv->card_type >= NV_C0)
  571. return 0;
  572. ret = nouveau_gpuobj_new(dev, chan,
  573. nouveau_gpuobj_class_instmem_size(dev, class),
  574. 16,
  575. NVOBJ_FLAG_ZERO_ALLOC | NVOBJ_FLAG_ZERO_FREE,
  576. &gpuobj);
  577. if (ret) {
  578. NV_ERROR(dev, "error creating gpuobj: %d\n", ret);
  579. return ret;
  580. }
  581. if (dev_priv->card_type >= NV_50) {
  582. nv_wo32(gpuobj, 0, class);
  583. nv_wo32(gpuobj, 20, 0x00010000);
  584. } else {
  585. switch (class) {
  586. case NV_CLASS_NULL:
  587. nv_wo32(gpuobj, 0, 0x00001030);
  588. nv_wo32(gpuobj, 4, 0xFFFFFFFF);
  589. break;
  590. default:
  591. if (dev_priv->card_type >= NV_40) {
  592. nv_wo32(gpuobj, 0, class);
  593. #ifdef __BIG_ENDIAN
  594. nv_wo32(gpuobj, 8, 0x01000000);
  595. #endif
  596. } else {
  597. #ifdef __BIG_ENDIAN
  598. nv_wo32(gpuobj, 0, class | 0x00080000);
  599. #else
  600. nv_wo32(gpuobj, 0, class);
  601. #endif
  602. }
  603. }
  604. }
  605. dev_priv->engine.instmem.flush(dev);
  606. gpuobj->engine = oc->engine;
  607. gpuobj->class = oc->id;
  608. insert:
  609. ret = nouveau_ramht_insert(chan, handle, gpuobj);
  610. if (ret)
  611. NV_ERROR(dev, "error adding gpuobj to RAMHT: %d\n", ret);
  612. nouveau_gpuobj_ref(NULL, &gpuobj);
  613. return ret;
  614. }
  615. static int
  616. nouveau_gpuobj_channel_init_pramin(struct nouveau_channel *chan)
  617. {
  618. struct drm_device *dev = chan->dev;
  619. struct drm_nouveau_private *dev_priv = dev->dev_private;
  620. uint32_t size;
  621. uint32_t base;
  622. int ret;
  623. NV_DEBUG(dev, "ch%d\n", chan->id);
  624. /* Base amount for object storage (4KiB enough?) */
  625. size = 0x2000;
  626. base = 0;
  627. /* PGRAPH context */
  628. size += dev_priv->engine.graph.grctx_size;
  629. if (dev_priv->card_type == NV_50) {
  630. /* Various fixed table thingos */
  631. size += 0x1400; /* mostly unknown stuff */
  632. size += 0x4000; /* vm pd */
  633. base = 0x6000;
  634. /* RAMHT, not sure about setting size yet, 32KiB to be safe */
  635. size += 0x8000;
  636. /* RAMFC */
  637. size += 0x1000;
  638. }
  639. ret = nouveau_gpuobj_new(dev, NULL, size, 0x1000, 0, &chan->ramin);
  640. if (ret) {
  641. NV_ERROR(dev, "Error allocating channel PRAMIN: %d\n", ret);
  642. return ret;
  643. }
  644. ret = drm_mm_init(&chan->ramin_heap, base, size);
  645. if (ret) {
  646. NV_ERROR(dev, "Error creating PRAMIN heap: %d\n", ret);
  647. nouveau_gpuobj_ref(NULL, &chan->ramin);
  648. return ret;
  649. }
  650. return 0;
  651. }
  652. int
  653. nouveau_gpuobj_channel_init(struct nouveau_channel *chan,
  654. uint32_t vram_h, uint32_t tt_h)
  655. {
  656. struct drm_device *dev = chan->dev;
  657. struct drm_nouveau_private *dev_priv = dev->dev_private;
  658. struct nouveau_gpuobj *vram = NULL, *tt = NULL;
  659. int ret;
  660. NV_DEBUG(dev, "ch%d vram=0x%08x tt=0x%08x\n", chan->id, vram_h, tt_h);
  661. if (dev_priv->card_type == NV_C0) {
  662. struct nouveau_vm *vm = dev_priv->chan_vm;
  663. struct nouveau_vm_pgd *vpgd;
  664. ret = nouveau_gpuobj_new(dev, NULL, 4096, 0x1000, 0,
  665. &chan->ramin);
  666. if (ret)
  667. return ret;
  668. nouveau_vm_ref(vm, &chan->vm, NULL);
  669. vpgd = list_first_entry(&vm->pgd_list, struct nouveau_vm_pgd, head);
  670. nv_wo32(chan->ramin, 0x0200, lower_32_bits(vpgd->obj->vinst));
  671. nv_wo32(chan->ramin, 0x0204, upper_32_bits(vpgd->obj->vinst));
  672. nv_wo32(chan->ramin, 0x0208, 0xffffffff);
  673. nv_wo32(chan->ramin, 0x020c, 0x000000ff);
  674. return 0;
  675. }
  676. /* Allocate a chunk of memory for per-channel object storage */
  677. ret = nouveau_gpuobj_channel_init_pramin(chan);
  678. if (ret) {
  679. NV_ERROR(dev, "init pramin\n");
  680. return ret;
  681. }
  682. /* NV50 VM
  683. * - Allocate per-channel page-directory
  684. * - Link with shared channel VM
  685. */
  686. if (dev_priv->chan_vm) {
  687. u32 pgd_offs = (dev_priv->chipset == 0x50) ? 0x1400 : 0x0200;
  688. u64 vm_vinst = chan->ramin->vinst + pgd_offs;
  689. u32 vm_pinst = chan->ramin->pinst;
  690. if (vm_pinst != ~0)
  691. vm_pinst += pgd_offs;
  692. ret = nouveau_gpuobj_new_fake(dev, vm_pinst, vm_vinst, 0x4000,
  693. 0, &chan->vm_pd);
  694. if (ret)
  695. return ret;
  696. nouveau_vm_ref(dev_priv->chan_vm, &chan->vm, chan->vm_pd);
  697. }
  698. /* RAMHT */
  699. if (dev_priv->card_type < NV_50) {
  700. nouveau_ramht_ref(dev_priv->ramht, &chan->ramht, NULL);
  701. } else {
  702. struct nouveau_gpuobj *ramht = NULL;
  703. ret = nouveau_gpuobj_new(dev, chan, 0x8000, 16,
  704. NVOBJ_FLAG_ZERO_ALLOC, &ramht);
  705. if (ret)
  706. return ret;
  707. ret = nouveau_ramht_new(dev, ramht, &chan->ramht);
  708. nouveau_gpuobj_ref(NULL, &ramht);
  709. if (ret)
  710. return ret;
  711. }
  712. /* VRAM ctxdma */
  713. if (dev_priv->card_type >= NV_50) {
  714. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
  715. 0, (1ULL << 40), 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, (1ULL << 40), NV_MEM_ACCESS_RW,
  741. NV_MEM_TARGET_VM, &tt);
  742. } else {
  743. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
  744. 0, dev_priv->gart_info.aper_size,
  745. NV_MEM_ACCESS_RW,
  746. NV_MEM_TARGET_GART, &tt);
  747. }
  748. if (ret) {
  749. NV_ERROR(dev, "Error creating TT ctxdma: %d\n", ret);
  750. return ret;
  751. }
  752. ret = nouveau_ramht_insert(chan, tt_h, tt);
  753. nouveau_gpuobj_ref(NULL, &tt);
  754. if (ret) {
  755. NV_ERROR(dev, "Error adding TT ctxdma to RAMHT: %d\n", ret);
  756. return ret;
  757. }
  758. return 0;
  759. }
  760. void
  761. nouveau_gpuobj_channel_takedown(struct nouveau_channel *chan)
  762. {
  763. struct drm_device *dev = chan->dev;
  764. NV_DEBUG(dev, "ch%d\n", chan->id);
  765. nouveau_ramht_ref(NULL, &chan->ramht, chan);
  766. nouveau_vm_ref(NULL, &chan->vm, chan->vm_pd);
  767. nouveau_gpuobj_ref(NULL, &chan->vm_pd);
  768. if (chan->ramin_heap.free_stack.next)
  769. drm_mm_takedown(&chan->ramin_heap);
  770. nouveau_gpuobj_ref(NULL, &chan->ramin);
  771. }
  772. int
  773. nouveau_gpuobj_suspend(struct drm_device *dev)
  774. {
  775. struct drm_nouveau_private *dev_priv = dev->dev_private;
  776. struct nouveau_gpuobj *gpuobj;
  777. int i;
  778. list_for_each_entry(gpuobj, &dev_priv->gpuobj_list, list) {
  779. if (gpuobj->cinst != NVOBJ_CINST_GLOBAL)
  780. continue;
  781. gpuobj->suspend = vmalloc(gpuobj->size);
  782. if (!gpuobj->suspend) {
  783. nouveau_gpuobj_resume(dev);
  784. return -ENOMEM;
  785. }
  786. for (i = 0; i < gpuobj->size; i += 4)
  787. gpuobj->suspend[i/4] = nv_ro32(gpuobj, i);
  788. }
  789. return 0;
  790. }
  791. void
  792. nouveau_gpuobj_resume(struct drm_device *dev)
  793. {
  794. struct drm_nouveau_private *dev_priv = dev->dev_private;
  795. struct nouveau_gpuobj *gpuobj;
  796. int i;
  797. list_for_each_entry(gpuobj, &dev_priv->gpuobj_list, list) {
  798. if (!gpuobj->suspend)
  799. continue;
  800. for (i = 0; i < gpuobj->size; i += 4)
  801. nv_wo32(gpuobj, i, gpuobj->suspend[i/4]);
  802. vfree(gpuobj->suspend);
  803. gpuobj->suspend = NULL;
  804. }
  805. dev_priv->engine.instmem.flush(dev);
  806. }
  807. int nouveau_ioctl_grobj_alloc(struct drm_device *dev, void *data,
  808. struct drm_file *file_priv)
  809. {
  810. struct drm_nouveau_grobj_alloc *init = data;
  811. struct nouveau_channel *chan;
  812. int ret;
  813. if (init->handle == ~0)
  814. return -EINVAL;
  815. chan = nouveau_channel_get(dev, file_priv, init->channel);
  816. if (IS_ERR(chan))
  817. return PTR_ERR(chan);
  818. if (nouveau_ramht_find(chan, init->handle)) {
  819. ret = -EEXIST;
  820. goto out;
  821. }
  822. ret = nouveau_gpuobj_gr_new(chan, init->handle, init->class);
  823. if (ret) {
  824. NV_ERROR(dev, "Error creating object: %d (%d/0x%08x)\n",
  825. ret, init->channel, init->handle);
  826. }
  827. out:
  828. nouveau_channel_put(&chan);
  829. return ret;
  830. }
  831. int nouveau_ioctl_gpuobj_free(struct drm_device *dev, void *data,
  832. struct drm_file *file_priv)
  833. {
  834. struct drm_nouveau_gpuobj_free *objfree = data;
  835. struct nouveau_channel *chan;
  836. int ret;
  837. chan = nouveau_channel_get(dev, file_priv, objfree->channel);
  838. if (IS_ERR(chan))
  839. return PTR_ERR(chan);
  840. /* Synchronize with the user channel */
  841. nouveau_channel_idle(chan);
  842. ret = nouveau_ramht_remove(chan, objfree->handle);
  843. nouveau_channel_put(&chan);
  844. return ret;
  845. }
  846. u32
  847. nv_ro32(struct nouveau_gpuobj *gpuobj, u32 offset)
  848. {
  849. struct drm_nouveau_private *dev_priv = gpuobj->dev->dev_private;
  850. struct drm_device *dev = gpuobj->dev;
  851. if (gpuobj->pinst == ~0 || !dev_priv->ramin_available) {
  852. u64 ptr = gpuobj->vinst + offset;
  853. u32 base = ptr >> 16;
  854. u32 val;
  855. spin_lock(&dev_priv->ramin_lock);
  856. if (dev_priv->ramin_base != base) {
  857. dev_priv->ramin_base = base;
  858. nv_wr32(dev, 0x001700, dev_priv->ramin_base);
  859. }
  860. val = nv_rd32(dev, 0x700000 + (ptr & 0xffff));
  861. spin_unlock(&dev_priv->ramin_lock);
  862. return val;
  863. }
  864. return nv_ri32(dev, gpuobj->pinst + offset);
  865. }
  866. void
  867. nv_wo32(struct nouveau_gpuobj *gpuobj, u32 offset, u32 val)
  868. {
  869. struct drm_nouveau_private *dev_priv = gpuobj->dev->dev_private;
  870. struct drm_device *dev = gpuobj->dev;
  871. if (gpuobj->pinst == ~0 || !dev_priv->ramin_available) {
  872. u64 ptr = gpuobj->vinst + offset;
  873. u32 base = ptr >> 16;
  874. spin_lock(&dev_priv->ramin_lock);
  875. if (dev_priv->ramin_base != base) {
  876. dev_priv->ramin_base = base;
  877. nv_wr32(dev, 0x001700, dev_priv->ramin_base);
  878. }
  879. nv_wr32(dev, 0x700000 + (ptr & 0xffff), val);
  880. spin_unlock(&dev_priv->ramin_lock);
  881. return;
  882. }
  883. nv_wi32(dev, gpuobj->pinst + offset, val);
  884. }