gtt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*
  2. * Copyright (c) 2007, Intel Corporation.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  17. *
  18. * Authors: Thomas Hellstrom <thomas-at-tungstengraphics.com>
  19. * Alan Cox <alan@linux.intel.com>
  20. */
  21. #include <drm/drmP.h>
  22. #include <linux/shmem_fs.h>
  23. #include "psb_drv.h"
  24. /*
  25. * GTT resource allocator - manage page mappings in GTT space
  26. */
  27. /**
  28. * psb_gtt_mask_pte - generate GTT pte entry
  29. * @pfn: page number to encode
  30. * @type: type of memory in the GTT
  31. *
  32. * Set the GTT entry for the appropriate memory type.
  33. */
  34. static inline uint32_t psb_gtt_mask_pte(uint32_t pfn, int type)
  35. {
  36. uint32_t mask = PSB_PTE_VALID;
  37. if (type & PSB_MMU_CACHED_MEMORY)
  38. mask |= PSB_PTE_CACHED;
  39. if (type & PSB_MMU_RO_MEMORY)
  40. mask |= PSB_PTE_RO;
  41. if (type & PSB_MMU_WO_MEMORY)
  42. mask |= PSB_PTE_WO;
  43. return (pfn << PAGE_SHIFT) | mask;
  44. }
  45. /**
  46. * psb_gtt_entry - find the GTT entries for a gtt_range
  47. * @dev: our DRM device
  48. * @r: our GTT range
  49. *
  50. * Given a gtt_range object return the GTT offset of the page table
  51. * entries for this gtt_range
  52. */
  53. static u32 *psb_gtt_entry(struct drm_device *dev, struct gtt_range *r)
  54. {
  55. struct drm_psb_private *dev_priv = dev->dev_private;
  56. unsigned long offset;
  57. offset = r->resource.start - dev_priv->gtt_mem->start;
  58. return dev_priv->gtt_map + (offset >> PAGE_SHIFT);
  59. }
  60. /**
  61. * psb_gtt_insert - put an object into the GTT
  62. * @dev: our DRM device
  63. * @r: our GTT range
  64. *
  65. * Take our preallocated GTT range and insert the GEM object into
  66. * the GTT. This is protected via the gtt mutex which the caller
  67. * must hold.
  68. */
  69. static int psb_gtt_insert(struct drm_device *dev, struct gtt_range *r)
  70. {
  71. u32 *gtt_slot, pte;
  72. struct page **pages;
  73. int i;
  74. if (r->pages == NULL) {
  75. WARN_ON(1);
  76. return -EINVAL;
  77. }
  78. WARN_ON(r->stolen); /* refcount these maybe ? */
  79. gtt_slot = psb_gtt_entry(dev, r);
  80. pages = r->pages;
  81. /* Make sure changes are visible to the GPU */
  82. set_pages_array_uc(pages, r->npage);
  83. /* Write our page entries into the GTT itself */
  84. for (i = r->roll; i < r->npage; i++) {
  85. pte = psb_gtt_mask_pte(page_to_pfn(r->pages[i]), 0);
  86. iowrite32(pte, gtt_slot++);
  87. }
  88. for (i = 0; i < r->roll; i++) {
  89. pte = psb_gtt_mask_pte(page_to_pfn(r->pages[i]), 0);
  90. iowrite32(pte, gtt_slot++);
  91. }
  92. /* Make sure all the entries are set before we return */
  93. ioread32(gtt_slot - 1);
  94. return 0;
  95. }
  96. /**
  97. * psb_gtt_remove - remove an object from the GTT
  98. * @dev: our DRM device
  99. * @r: our GTT range
  100. *
  101. * Remove a preallocated GTT range from the GTT. Overwrite all the
  102. * page table entries with the dummy page. This is protected via the gtt
  103. * mutex which the caller must hold.
  104. */
  105. static void psb_gtt_remove(struct drm_device *dev, struct gtt_range *r)
  106. {
  107. struct drm_psb_private *dev_priv = dev->dev_private;
  108. u32 *gtt_slot, pte;
  109. int i;
  110. WARN_ON(r->stolen);
  111. gtt_slot = psb_gtt_entry(dev, r);
  112. pte = psb_gtt_mask_pte(page_to_pfn(dev_priv->scratch_page), 0);
  113. for (i = 0; i < r->npage; i++)
  114. iowrite32(pte, gtt_slot++);
  115. ioread32(gtt_slot - 1);
  116. set_pages_array_wb(r->pages, r->npage);
  117. }
  118. /**
  119. * psb_gtt_roll - set scrolling position
  120. * @dev: our DRM device
  121. * @r: the gtt mapping we are using
  122. * @roll: roll offset
  123. *
  124. * Roll an existing pinned mapping by moving the pages through the GTT.
  125. * This allows us to implement hardware scrolling on the consoles without
  126. * a 2D engine
  127. */
  128. void psb_gtt_roll(struct drm_device *dev, struct gtt_range *r, int roll)
  129. {
  130. u32 *gtt_slot, pte;
  131. int i;
  132. if (roll >= r->npage) {
  133. WARN_ON(1);
  134. return;
  135. }
  136. r->roll = roll;
  137. /* Not currently in the GTT - no worry we will write the mapping at
  138. the right position when it gets pinned */
  139. if (!r->stolen && !r->in_gart)
  140. return;
  141. gtt_slot = psb_gtt_entry(dev, r);
  142. for (i = r->roll; i < r->npage; i++) {
  143. pte = psb_gtt_mask_pte(page_to_pfn(r->pages[i]), 0);
  144. iowrite32(pte, gtt_slot++);
  145. }
  146. for (i = 0; i < r->roll; i++) {
  147. pte = psb_gtt_mask_pte(page_to_pfn(r->pages[i]), 0);
  148. iowrite32(pte, gtt_slot++);
  149. }
  150. ioread32(gtt_slot - 1);
  151. }
  152. /**
  153. * psb_gtt_attach_pages - attach and pin GEM pages
  154. * @gt: the gtt range
  155. *
  156. * Pin and build an in kernel list of the pages that back our GEM object.
  157. * While we hold this the pages cannot be swapped out. This is protected
  158. * via the gtt mutex which the caller must hold.
  159. */
  160. static int psb_gtt_attach_pages(struct gtt_range *gt)
  161. {
  162. struct inode *inode;
  163. struct address_space *mapping;
  164. int i;
  165. struct page *p;
  166. int pages = gt->gem.size / PAGE_SIZE;
  167. WARN_ON(gt->pages);
  168. /* This is the shared memory object that backs the GEM resource */
  169. inode = gt->gem.filp->f_path.dentry->d_inode;
  170. mapping = inode->i_mapping;
  171. gt->pages = kmalloc(pages * sizeof(struct page *), GFP_KERNEL);
  172. if (gt->pages == NULL)
  173. return -ENOMEM;
  174. gt->npage = pages;
  175. for (i = 0; i < pages; i++) {
  176. p = shmem_read_mapping_page(mapping, i);
  177. if (IS_ERR(p))
  178. goto err;
  179. gt->pages[i] = p;
  180. }
  181. return 0;
  182. err:
  183. while (i--)
  184. page_cache_release(gt->pages[i]);
  185. kfree(gt->pages);
  186. gt->pages = NULL;
  187. return PTR_ERR(p);
  188. }
  189. /**
  190. * psb_gtt_detach_pages - attach and pin GEM pages
  191. * @gt: the gtt range
  192. *
  193. * Undo the effect of psb_gtt_attach_pages. At this point the pages
  194. * must have been removed from the GTT as they could now be paged out
  195. * and move bus address. This is protected via the gtt mutex which the
  196. * caller must hold.
  197. */
  198. static void psb_gtt_detach_pages(struct gtt_range *gt)
  199. {
  200. int i;
  201. for (i = 0; i < gt->npage; i++) {
  202. /* FIXME: do we need to force dirty */
  203. set_page_dirty(gt->pages[i]);
  204. page_cache_release(gt->pages[i]);
  205. }
  206. kfree(gt->pages);
  207. gt->pages = NULL;
  208. }
  209. /**
  210. * psb_gtt_pin - pin pages into the GTT
  211. * @gt: range to pin
  212. *
  213. * Pin a set of pages into the GTT. The pins are refcounted so that
  214. * multiple pins need multiple unpins to undo.
  215. *
  216. * Non GEM backed objects treat this as a no-op as they are always GTT
  217. * backed objects.
  218. */
  219. int psb_gtt_pin(struct gtt_range *gt)
  220. {
  221. int ret = 0;
  222. struct drm_device *dev = gt->gem.dev;
  223. struct drm_psb_private *dev_priv = dev->dev_private;
  224. mutex_lock(&dev_priv->gtt_mutex);
  225. if (gt->in_gart == 0 && gt->stolen == 0) {
  226. ret = psb_gtt_attach_pages(gt);
  227. if (ret < 0)
  228. goto out;
  229. ret = psb_gtt_insert(dev, gt);
  230. if (ret < 0) {
  231. psb_gtt_detach_pages(gt);
  232. goto out;
  233. }
  234. }
  235. gt->in_gart++;
  236. out:
  237. mutex_unlock(&dev_priv->gtt_mutex);
  238. return ret;
  239. }
  240. /**
  241. * psb_gtt_unpin - Drop a GTT pin requirement
  242. * @gt: range to pin
  243. *
  244. * Undoes the effect of psb_gtt_pin. On the last drop the GEM object
  245. * will be removed from the GTT which will also drop the page references
  246. * and allow the VM to clean up or page stuff.
  247. *
  248. * Non GEM backed objects treat this as a no-op as they are always GTT
  249. * backed objects.
  250. */
  251. void psb_gtt_unpin(struct gtt_range *gt)
  252. {
  253. struct drm_device *dev = gt->gem.dev;
  254. struct drm_psb_private *dev_priv = dev->dev_private;
  255. mutex_lock(&dev_priv->gtt_mutex);
  256. WARN_ON(!gt->in_gart);
  257. gt->in_gart--;
  258. if (gt->in_gart == 0 && gt->stolen == 0) {
  259. psb_gtt_remove(dev, gt);
  260. psb_gtt_detach_pages(gt);
  261. }
  262. mutex_unlock(&dev_priv->gtt_mutex);
  263. }
  264. /*
  265. * GTT resource allocator - allocate and manage GTT address space
  266. */
  267. /**
  268. * psb_gtt_alloc_range - allocate GTT address space
  269. * @dev: Our DRM device
  270. * @len: length (bytes) of address space required
  271. * @name: resource name
  272. * @backed: resource should be backed by stolen pages
  273. *
  274. * Ask the kernel core to find us a suitable range of addresses
  275. * to use for a GTT mapping.
  276. *
  277. * Returns a gtt_range structure describing the object, or NULL on
  278. * error. On successful return the resource is both allocated and marked
  279. * as in use.
  280. */
  281. struct gtt_range *psb_gtt_alloc_range(struct drm_device *dev, int len,
  282. const char *name, int backed)
  283. {
  284. struct drm_psb_private *dev_priv = dev->dev_private;
  285. struct gtt_range *gt;
  286. struct resource *r = dev_priv->gtt_mem;
  287. int ret;
  288. unsigned long start, end;
  289. if (backed) {
  290. /* The start of the GTT is the stolen pages */
  291. start = r->start;
  292. end = r->start + dev_priv->gtt.stolen_size - 1;
  293. } else {
  294. /* The rest we will use for GEM backed objects */
  295. start = r->start + dev_priv->gtt.stolen_size;
  296. end = r->end;
  297. }
  298. gt = kzalloc(sizeof(struct gtt_range), GFP_KERNEL);
  299. if (gt == NULL)
  300. return NULL;
  301. gt->resource.name = name;
  302. gt->stolen = backed;
  303. gt->in_gart = backed;
  304. gt->roll = 0;
  305. /* Ensure this is set for non GEM objects */
  306. gt->gem.dev = dev;
  307. ret = allocate_resource(dev_priv->gtt_mem, &gt->resource,
  308. len, start, end, PAGE_SIZE, NULL, NULL);
  309. if (ret == 0) {
  310. gt->offset = gt->resource.start - r->start;
  311. return gt;
  312. }
  313. kfree(gt);
  314. return NULL;
  315. }
  316. /**
  317. * psb_gtt_free_range - release GTT address space
  318. * @dev: our DRM device
  319. * @gt: a mapping created with psb_gtt_alloc_range
  320. *
  321. * Release a resource that was allocated with psb_gtt_alloc_range. If the
  322. * object has been pinned by mmap users we clean this up here currently.
  323. */
  324. void psb_gtt_free_range(struct drm_device *dev, struct gtt_range *gt)
  325. {
  326. /* Undo the mmap pin if we are destroying the object */
  327. if (gt->mmapping) {
  328. psb_gtt_unpin(gt);
  329. gt->mmapping = 0;
  330. }
  331. WARN_ON(gt->in_gart && !gt->stolen);
  332. release_resource(&gt->resource);
  333. kfree(gt);
  334. }
  335. static void psb_gtt_alloc(struct drm_device *dev)
  336. {
  337. struct drm_psb_private *dev_priv = dev->dev_private;
  338. init_rwsem(&dev_priv->gtt.sem);
  339. }
  340. void psb_gtt_takedown(struct drm_device *dev)
  341. {
  342. struct drm_psb_private *dev_priv = dev->dev_private;
  343. if (dev_priv->gtt_map) {
  344. iounmap(dev_priv->gtt_map);
  345. dev_priv->gtt_map = NULL;
  346. }
  347. if (dev_priv->gtt_initialized) {
  348. pci_write_config_word(dev->pdev, PSB_GMCH_CTRL,
  349. dev_priv->gmch_ctrl);
  350. PSB_WVDC32(dev_priv->pge_ctl, PSB_PGETBL_CTL);
  351. (void) PSB_RVDC32(PSB_PGETBL_CTL);
  352. }
  353. if (dev_priv->vram_addr)
  354. iounmap(dev_priv->gtt_map);
  355. }
  356. int psb_gtt_init(struct drm_device *dev, int resume)
  357. {
  358. struct drm_psb_private *dev_priv = dev->dev_private;
  359. unsigned gtt_pages;
  360. unsigned long stolen_size, vram_stolen_size;
  361. unsigned i, num_pages;
  362. unsigned pfn_base;
  363. uint32_t vram_pages;
  364. uint32_t dvmt_mode = 0;
  365. struct psb_gtt *pg;
  366. int ret = 0;
  367. uint32_t pte;
  368. mutex_init(&dev_priv->gtt_mutex);
  369. psb_gtt_alloc(dev);
  370. pg = &dev_priv->gtt;
  371. /* Enable the GTT */
  372. pci_read_config_word(dev->pdev, PSB_GMCH_CTRL, &dev_priv->gmch_ctrl);
  373. pci_write_config_word(dev->pdev, PSB_GMCH_CTRL,
  374. dev_priv->gmch_ctrl | _PSB_GMCH_ENABLED);
  375. dev_priv->pge_ctl = PSB_RVDC32(PSB_PGETBL_CTL);
  376. PSB_WVDC32(dev_priv->pge_ctl | _PSB_PGETBL_ENABLED, PSB_PGETBL_CTL);
  377. (void) PSB_RVDC32(PSB_PGETBL_CTL);
  378. /* The root resource we allocate address space from */
  379. dev_priv->gtt_initialized = 1;
  380. pg->gtt_phys_start = dev_priv->pge_ctl & PAGE_MASK;
  381. /*
  382. * The video mmu has a hw bug when accessing 0x0D0000000.
  383. * Make gatt start at 0x0e000,0000. This doesn't actually
  384. * matter for us but may do if the video acceleration ever
  385. * gets opened up.
  386. */
  387. pg->mmu_gatt_start = 0xE0000000;
  388. pg->gtt_start = pci_resource_start(dev->pdev, PSB_GTT_RESOURCE);
  389. gtt_pages = pci_resource_len(dev->pdev, PSB_GTT_RESOURCE)
  390. >> PAGE_SHIFT;
  391. /* CDV doesn't report this. In which case the system has 64 gtt pages */
  392. if (pg->gtt_start == 0 || gtt_pages == 0) {
  393. dev_dbg(dev->dev, "GTT PCI BAR not initialized.\n");
  394. gtt_pages = 64;
  395. pg->gtt_start = dev_priv->pge_ctl;
  396. }
  397. pg->gatt_start = pci_resource_start(dev->pdev, PSB_GATT_RESOURCE);
  398. pg->gatt_pages = pci_resource_len(dev->pdev, PSB_GATT_RESOURCE)
  399. >> PAGE_SHIFT;
  400. dev_priv->gtt_mem = &dev->pdev->resource[PSB_GATT_RESOURCE];
  401. if (pg->gatt_pages == 0 || pg->gatt_start == 0) {
  402. static struct resource fudge; /* Preferably peppermint */
  403. /* This can occur on CDV systems. Fudge it in this case.
  404. We really don't care what imaginary space is being allocated
  405. at this point */
  406. dev_dbg(dev->dev, "GATT PCI BAR not initialized.\n");
  407. pg->gatt_start = 0x40000000;
  408. pg->gatt_pages = (128 * 1024 * 1024) >> PAGE_SHIFT;
  409. /* This is a little confusing but in fact the GTT is providing
  410. a view from the GPU into memory and not vice versa. As such
  411. this is really allocating space that is not the same as the
  412. CPU address space on CDV */
  413. fudge.start = 0x40000000;
  414. fudge.end = 0x40000000 + 128 * 1024 * 1024 - 1;
  415. fudge.name = "fudge";
  416. fudge.flags = IORESOURCE_MEM;
  417. dev_priv->gtt_mem = &fudge;
  418. }
  419. pci_read_config_dword(dev->pdev, PSB_BSM, &dev_priv->stolen_base);
  420. vram_stolen_size = pg->gtt_phys_start - dev_priv->stolen_base
  421. - PAGE_SIZE;
  422. stolen_size = vram_stolen_size;
  423. printk(KERN_INFO "Stolen memory information\n");
  424. printk(KERN_INFO " base in RAM: 0x%x\n", dev_priv->stolen_base);
  425. printk(KERN_INFO " size: %luK, calculated by (GTT RAM base) - (Stolen base), seems wrong\n",
  426. vram_stolen_size/1024);
  427. dvmt_mode = (dev_priv->gmch_ctrl >> 4) & 0x7;
  428. printk(KERN_INFO " the correct size should be: %dM(dvmt mode=%d)\n",
  429. (dvmt_mode == 1) ? 1 : (2 << (dvmt_mode - 1)), dvmt_mode);
  430. if (resume && (gtt_pages != pg->gtt_pages) &&
  431. (stolen_size != pg->stolen_size)) {
  432. dev_err(dev->dev, "GTT resume error.\n");
  433. ret = -EINVAL;
  434. goto out_err;
  435. }
  436. pg->gtt_pages = gtt_pages;
  437. pg->stolen_size = stolen_size;
  438. dev_priv->vram_stolen_size = vram_stolen_size;
  439. /*
  440. * Map the GTT and the stolen memory area
  441. */
  442. dev_priv->gtt_map = ioremap_nocache(pg->gtt_phys_start,
  443. gtt_pages << PAGE_SHIFT);
  444. if (!dev_priv->gtt_map) {
  445. dev_err(dev->dev, "Failure to map gtt.\n");
  446. ret = -ENOMEM;
  447. goto out_err;
  448. }
  449. dev_priv->vram_addr = ioremap_wc(dev_priv->stolen_base, stolen_size);
  450. if (!dev_priv->vram_addr) {
  451. dev_err(dev->dev, "Failure to map stolen base.\n");
  452. ret = -ENOMEM;
  453. goto out_err;
  454. }
  455. /*
  456. * Insert vram stolen pages into the GTT
  457. */
  458. pfn_base = dev_priv->stolen_base >> PAGE_SHIFT;
  459. vram_pages = num_pages = vram_stolen_size >> PAGE_SHIFT;
  460. printk(KERN_INFO"Set up %d stolen pages starting at 0x%08x, GTT offset %dK\n",
  461. num_pages, pfn_base << PAGE_SHIFT, 0);
  462. for (i = 0; i < num_pages; ++i) {
  463. pte = psb_gtt_mask_pte(pfn_base + i, 0);
  464. iowrite32(pte, dev_priv->gtt_map + i);
  465. }
  466. /*
  467. * Init rest of GTT to the scratch page to avoid accidents or scribbles
  468. */
  469. pfn_base = page_to_pfn(dev_priv->scratch_page);
  470. pte = psb_gtt_mask_pte(pfn_base, 0);
  471. for (; i < gtt_pages; ++i)
  472. iowrite32(pte, dev_priv->gtt_map + i);
  473. (void) ioread32(dev_priv->gtt_map + i - 1);
  474. return 0;
  475. out_err:
  476. psb_gtt_takedown(dev);
  477. return ret;
  478. }