gtt.c 16 KB

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