gtt.c 15 KB

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