nouveau_object.c 24 KB

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