gtt.c 14 KB

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