intel-gtt.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  1. /*
  2. * Intel GTT (Graphics Translation Table) routines
  3. *
  4. * Caveat: This driver implements the linux agp interface, but this is far from
  5. * a agp driver! GTT support ended up here for purely historical reasons: The
  6. * old userspace intel graphics drivers needed an interface to map memory into
  7. * the GTT. And the drm provides a default interface for graphic devices sitting
  8. * on an agp port. So it made sense to fake the GTT support as an agp port to
  9. * avoid having to create a new api.
  10. *
  11. * With gem this does not make much sense anymore, just needlessly complicates
  12. * the code. But as long as the old graphics stack is still support, it's stuck
  13. * here.
  14. *
  15. * /fairy-tale-mode off
  16. */
  17. #include <linux/module.h>
  18. #include <linux/pci.h>
  19. #include <linux/kernel.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/agp_backend.h>
  22. #include <linux/delay.h>
  23. #include <asm/smp.h>
  24. #include "agp.h"
  25. #include "intel-agp.h"
  26. #include <drm/intel-gtt.h>
  27. /*
  28. * If we have Intel graphics, we're not going to have anything other than
  29. * an Intel IOMMU. So make the correct use of the PCI DMA API contingent
  30. * on the Intel IOMMU support (CONFIG_INTEL_IOMMU).
  31. * Only newer chipsets need to bother with this, of course.
  32. */
  33. #ifdef CONFIG_INTEL_IOMMU
  34. #define USE_PCI_DMA_API 1
  35. #else
  36. #define USE_PCI_DMA_API 0
  37. #endif
  38. struct intel_gtt_driver {
  39. unsigned int gen : 8;
  40. unsigned int is_g33 : 1;
  41. unsigned int is_pineview : 1;
  42. unsigned int is_ironlake : 1;
  43. unsigned int has_pgtbl_enable : 1;
  44. unsigned int dma_mask_size : 8;
  45. /* Chipset specific GTT setup */
  46. int (*setup)(void);
  47. /* This should undo anything done in ->setup() save the unmapping
  48. * of the mmio register file, that's done in the generic code. */
  49. void (*cleanup)(void);
  50. void (*write_entry)(dma_addr_t addr, unsigned int entry, unsigned int flags);
  51. /* Flags is a more or less chipset specific opaque value.
  52. * For chipsets that need to support old ums (non-gem) code, this
  53. * needs to be identical to the various supported agp memory types! */
  54. bool (*check_flags)(unsigned int flags);
  55. void (*chipset_flush)(void);
  56. };
  57. static struct _intel_private {
  58. const struct intel_gtt_driver *driver;
  59. struct pci_dev *pcidev; /* device one */
  60. struct pci_dev *bridge_dev;
  61. u8 __iomem *registers;
  62. phys_addr_t gtt_phys_addr;
  63. u32 PGETBL_save;
  64. u32 __iomem *gtt; /* I915G */
  65. bool clear_fake_agp; /* on first access via agp, fill with scratch */
  66. int num_dcache_entries;
  67. void __iomem *i9xx_flush_page;
  68. char *i81x_gtt_table;
  69. struct resource ifp_resource;
  70. int resource_valid;
  71. struct page *scratch_page;
  72. phys_addr_t scratch_page_dma;
  73. int refcount;
  74. /* Whether i915 needs to use the dmar apis or not. */
  75. unsigned int needs_dmar : 1;
  76. phys_addr_t gma_bus_addr;
  77. /* Size of memory reserved for graphics by the BIOS */
  78. unsigned int stolen_size;
  79. /* Total number of gtt entries. */
  80. unsigned int gtt_total_entries;
  81. /* Part of the gtt that is mappable by the cpu, for those chips where
  82. * this is not the full gtt. */
  83. unsigned int gtt_mappable_entries;
  84. } intel_private;
  85. #define INTEL_GTT_GEN intel_private.driver->gen
  86. #define IS_G33 intel_private.driver->is_g33
  87. #define IS_PINEVIEW intel_private.driver->is_pineview
  88. #define IS_IRONLAKE intel_private.driver->is_ironlake
  89. #define HAS_PGTBL_EN intel_private.driver->has_pgtbl_enable
  90. #if IS_ENABLED(CONFIG_AGP_INTEL)
  91. static int intel_gtt_map_memory(struct page **pages,
  92. unsigned int num_entries,
  93. struct sg_table *st)
  94. {
  95. struct scatterlist *sg;
  96. int i;
  97. DBG("try mapping %lu pages\n", (unsigned long)num_entries);
  98. if (sg_alloc_table(st, num_entries, GFP_KERNEL))
  99. goto err;
  100. for_each_sg(st->sgl, sg, num_entries, i)
  101. sg_set_page(sg, pages[i], PAGE_SIZE, 0);
  102. if (!pci_map_sg(intel_private.pcidev,
  103. st->sgl, st->nents, PCI_DMA_BIDIRECTIONAL))
  104. goto err;
  105. return 0;
  106. err:
  107. sg_free_table(st);
  108. return -ENOMEM;
  109. }
  110. static void intel_gtt_unmap_memory(struct scatterlist *sg_list, int num_sg)
  111. {
  112. struct sg_table st;
  113. DBG("try unmapping %lu pages\n", (unsigned long)mem->page_count);
  114. pci_unmap_sg(intel_private.pcidev, sg_list,
  115. num_sg, PCI_DMA_BIDIRECTIONAL);
  116. st.sgl = sg_list;
  117. st.orig_nents = st.nents = num_sg;
  118. sg_free_table(&st);
  119. }
  120. static void intel_fake_agp_enable(struct agp_bridge_data *bridge, u32 mode)
  121. {
  122. return;
  123. }
  124. /* Exists to support ARGB cursors */
  125. static struct page *i8xx_alloc_pages(void)
  126. {
  127. struct page *page;
  128. page = alloc_pages(GFP_KERNEL | GFP_DMA32, 2);
  129. if (page == NULL)
  130. return NULL;
  131. if (set_pages_uc(page, 4) < 0) {
  132. set_pages_wb(page, 4);
  133. __free_pages(page, 2);
  134. return NULL;
  135. }
  136. get_page(page);
  137. atomic_inc(&agp_bridge->current_memory_agp);
  138. return page;
  139. }
  140. static void i8xx_destroy_pages(struct page *page)
  141. {
  142. if (page == NULL)
  143. return;
  144. set_pages_wb(page, 4);
  145. put_page(page);
  146. __free_pages(page, 2);
  147. atomic_dec(&agp_bridge->current_memory_agp);
  148. }
  149. #endif
  150. #define I810_GTT_ORDER 4
  151. static int i810_setup(void)
  152. {
  153. phys_addr_t reg_addr;
  154. char *gtt_table;
  155. /* i81x does not preallocate the gtt. It's always 64kb in size. */
  156. gtt_table = alloc_gatt_pages(I810_GTT_ORDER);
  157. if (gtt_table == NULL)
  158. return -ENOMEM;
  159. intel_private.i81x_gtt_table = gtt_table;
  160. reg_addr = pci_resource_start(intel_private.pcidev, I810_MMADR_BAR);
  161. intel_private.registers = ioremap(reg_addr, KB(64));
  162. if (!intel_private.registers)
  163. return -ENOMEM;
  164. writel(virt_to_phys(gtt_table) | I810_PGETBL_ENABLED,
  165. intel_private.registers+I810_PGETBL_CTL);
  166. intel_private.gtt_phys_addr = reg_addr + I810_PTE_BASE;
  167. if ((readl(intel_private.registers+I810_DRAM_CTL)
  168. & I810_DRAM_ROW_0) == I810_DRAM_ROW_0_SDRAM) {
  169. dev_info(&intel_private.pcidev->dev,
  170. "detected 4MB dedicated video ram\n");
  171. intel_private.num_dcache_entries = 1024;
  172. }
  173. return 0;
  174. }
  175. static void i810_cleanup(void)
  176. {
  177. writel(0, intel_private.registers+I810_PGETBL_CTL);
  178. free_gatt_pages(intel_private.i81x_gtt_table, I810_GTT_ORDER);
  179. }
  180. #if IS_ENABLED(CONFIG_AGP_INTEL)
  181. static int i810_insert_dcache_entries(struct agp_memory *mem, off_t pg_start,
  182. int type)
  183. {
  184. int i;
  185. if ((pg_start + mem->page_count)
  186. > intel_private.num_dcache_entries)
  187. return -EINVAL;
  188. if (!mem->is_flushed)
  189. global_cache_flush();
  190. for (i = pg_start; i < (pg_start + mem->page_count); i++) {
  191. dma_addr_t addr = i << PAGE_SHIFT;
  192. intel_private.driver->write_entry(addr,
  193. i, type);
  194. }
  195. readl(intel_private.gtt+i-1);
  196. return 0;
  197. }
  198. /*
  199. * The i810/i830 requires a physical address to program its mouse
  200. * pointer into hardware.
  201. * However the Xserver still writes to it through the agp aperture.
  202. */
  203. static struct agp_memory *alloc_agpphysmem_i8xx(size_t pg_count, int type)
  204. {
  205. struct agp_memory *new;
  206. struct page *page;
  207. switch (pg_count) {
  208. case 1: page = agp_bridge->driver->agp_alloc_page(agp_bridge);
  209. break;
  210. case 4:
  211. /* kludge to get 4 physical pages for ARGB cursor */
  212. page = i8xx_alloc_pages();
  213. break;
  214. default:
  215. return NULL;
  216. }
  217. if (page == NULL)
  218. return NULL;
  219. new = agp_create_memory(pg_count);
  220. if (new == NULL)
  221. return NULL;
  222. new->pages[0] = page;
  223. if (pg_count == 4) {
  224. /* kludge to get 4 physical pages for ARGB cursor */
  225. new->pages[1] = new->pages[0] + 1;
  226. new->pages[2] = new->pages[1] + 1;
  227. new->pages[3] = new->pages[2] + 1;
  228. }
  229. new->page_count = pg_count;
  230. new->num_scratch_pages = pg_count;
  231. new->type = AGP_PHYS_MEMORY;
  232. new->physical = page_to_phys(new->pages[0]);
  233. return new;
  234. }
  235. static void intel_i810_free_by_type(struct agp_memory *curr)
  236. {
  237. agp_free_key(curr->key);
  238. if (curr->type == AGP_PHYS_MEMORY) {
  239. if (curr->page_count == 4)
  240. i8xx_destroy_pages(curr->pages[0]);
  241. else {
  242. agp_bridge->driver->agp_destroy_page(curr->pages[0],
  243. AGP_PAGE_DESTROY_UNMAP);
  244. agp_bridge->driver->agp_destroy_page(curr->pages[0],
  245. AGP_PAGE_DESTROY_FREE);
  246. }
  247. agp_free_page_array(curr);
  248. }
  249. kfree(curr);
  250. }
  251. #endif
  252. static int intel_gtt_setup_scratch_page(void)
  253. {
  254. struct page *page;
  255. dma_addr_t dma_addr;
  256. page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
  257. if (page == NULL)
  258. return -ENOMEM;
  259. get_page(page);
  260. set_pages_uc(page, 1);
  261. if (intel_private.needs_dmar) {
  262. dma_addr = pci_map_page(intel_private.pcidev, page, 0,
  263. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  264. if (pci_dma_mapping_error(intel_private.pcidev, dma_addr))
  265. return -EINVAL;
  266. intel_private.scratch_page_dma = dma_addr;
  267. } else
  268. intel_private.scratch_page_dma = page_to_phys(page);
  269. intel_private.scratch_page = page;
  270. return 0;
  271. }
  272. static void i810_write_entry(dma_addr_t addr, unsigned int entry,
  273. unsigned int flags)
  274. {
  275. u32 pte_flags = I810_PTE_VALID;
  276. switch (flags) {
  277. case AGP_DCACHE_MEMORY:
  278. pte_flags |= I810_PTE_LOCAL;
  279. break;
  280. case AGP_USER_CACHED_MEMORY:
  281. pte_flags |= I830_PTE_SYSTEM_CACHED;
  282. break;
  283. }
  284. writel(addr | pte_flags, intel_private.gtt + entry);
  285. }
  286. static const struct aper_size_info_fixed intel_fake_agp_sizes[] = {
  287. {32, 8192, 3},
  288. {64, 16384, 4},
  289. {128, 32768, 5},
  290. {256, 65536, 6},
  291. {512, 131072, 7},
  292. };
  293. static unsigned int intel_gtt_stolen_size(void)
  294. {
  295. u16 gmch_ctrl;
  296. u8 rdct;
  297. int local = 0;
  298. static const int ddt[4] = { 0, 16, 32, 64 };
  299. unsigned int stolen_size = 0;
  300. if (INTEL_GTT_GEN == 1)
  301. return 0; /* no stolen mem on i81x */
  302. pci_read_config_word(intel_private.bridge_dev,
  303. I830_GMCH_CTRL, &gmch_ctrl);
  304. if (intel_private.bridge_dev->device == PCI_DEVICE_ID_INTEL_82830_HB ||
  305. intel_private.bridge_dev->device == PCI_DEVICE_ID_INTEL_82845G_HB) {
  306. switch (gmch_ctrl & I830_GMCH_GMS_MASK) {
  307. case I830_GMCH_GMS_STOLEN_512:
  308. stolen_size = KB(512);
  309. break;
  310. case I830_GMCH_GMS_STOLEN_1024:
  311. stolen_size = MB(1);
  312. break;
  313. case I830_GMCH_GMS_STOLEN_8192:
  314. stolen_size = MB(8);
  315. break;
  316. case I830_GMCH_GMS_LOCAL:
  317. rdct = readb(intel_private.registers+I830_RDRAM_CHANNEL_TYPE);
  318. stolen_size = (I830_RDRAM_ND(rdct) + 1) *
  319. MB(ddt[I830_RDRAM_DDT(rdct)]);
  320. local = 1;
  321. break;
  322. default:
  323. stolen_size = 0;
  324. break;
  325. }
  326. } else {
  327. switch (gmch_ctrl & I855_GMCH_GMS_MASK) {
  328. case I855_GMCH_GMS_STOLEN_1M:
  329. stolen_size = MB(1);
  330. break;
  331. case I855_GMCH_GMS_STOLEN_4M:
  332. stolen_size = MB(4);
  333. break;
  334. case I855_GMCH_GMS_STOLEN_8M:
  335. stolen_size = MB(8);
  336. break;
  337. case I855_GMCH_GMS_STOLEN_16M:
  338. stolen_size = MB(16);
  339. break;
  340. case I855_GMCH_GMS_STOLEN_32M:
  341. stolen_size = MB(32);
  342. break;
  343. case I915_GMCH_GMS_STOLEN_48M:
  344. stolen_size = MB(48);
  345. break;
  346. case I915_GMCH_GMS_STOLEN_64M:
  347. stolen_size = MB(64);
  348. break;
  349. case G33_GMCH_GMS_STOLEN_128M:
  350. stolen_size = MB(128);
  351. break;
  352. case G33_GMCH_GMS_STOLEN_256M:
  353. stolen_size = MB(256);
  354. break;
  355. case INTEL_GMCH_GMS_STOLEN_96M:
  356. stolen_size = MB(96);
  357. break;
  358. case INTEL_GMCH_GMS_STOLEN_160M:
  359. stolen_size = MB(160);
  360. break;
  361. case INTEL_GMCH_GMS_STOLEN_224M:
  362. stolen_size = MB(224);
  363. break;
  364. case INTEL_GMCH_GMS_STOLEN_352M:
  365. stolen_size = MB(352);
  366. break;
  367. default:
  368. stolen_size = 0;
  369. break;
  370. }
  371. }
  372. if (stolen_size > 0) {
  373. dev_info(&intel_private.bridge_dev->dev, "detected %dK %s memory\n",
  374. stolen_size / KB(1), local ? "local" : "stolen");
  375. } else {
  376. dev_info(&intel_private.bridge_dev->dev,
  377. "no pre-allocated video memory detected\n");
  378. stolen_size = 0;
  379. }
  380. return stolen_size;
  381. }
  382. static void i965_adjust_pgetbl_size(unsigned int size_flag)
  383. {
  384. u32 pgetbl_ctl, pgetbl_ctl2;
  385. /* ensure that ppgtt is disabled */
  386. pgetbl_ctl2 = readl(intel_private.registers+I965_PGETBL_CTL2);
  387. pgetbl_ctl2 &= ~I810_PGETBL_ENABLED;
  388. writel(pgetbl_ctl2, intel_private.registers+I965_PGETBL_CTL2);
  389. /* write the new ggtt size */
  390. pgetbl_ctl = readl(intel_private.registers+I810_PGETBL_CTL);
  391. pgetbl_ctl &= ~I965_PGETBL_SIZE_MASK;
  392. pgetbl_ctl |= size_flag;
  393. writel(pgetbl_ctl, intel_private.registers+I810_PGETBL_CTL);
  394. }
  395. static unsigned int i965_gtt_total_entries(void)
  396. {
  397. int size;
  398. u32 pgetbl_ctl;
  399. u16 gmch_ctl;
  400. pci_read_config_word(intel_private.bridge_dev,
  401. I830_GMCH_CTRL, &gmch_ctl);
  402. if (INTEL_GTT_GEN == 5) {
  403. switch (gmch_ctl & G4x_GMCH_SIZE_MASK) {
  404. case G4x_GMCH_SIZE_1M:
  405. case G4x_GMCH_SIZE_VT_1M:
  406. i965_adjust_pgetbl_size(I965_PGETBL_SIZE_1MB);
  407. break;
  408. case G4x_GMCH_SIZE_VT_1_5M:
  409. i965_adjust_pgetbl_size(I965_PGETBL_SIZE_1_5MB);
  410. break;
  411. case G4x_GMCH_SIZE_2M:
  412. case G4x_GMCH_SIZE_VT_2M:
  413. i965_adjust_pgetbl_size(I965_PGETBL_SIZE_2MB);
  414. break;
  415. }
  416. }
  417. pgetbl_ctl = readl(intel_private.registers+I810_PGETBL_CTL);
  418. switch (pgetbl_ctl & I965_PGETBL_SIZE_MASK) {
  419. case I965_PGETBL_SIZE_128KB:
  420. size = KB(128);
  421. break;
  422. case I965_PGETBL_SIZE_256KB:
  423. size = KB(256);
  424. break;
  425. case I965_PGETBL_SIZE_512KB:
  426. size = KB(512);
  427. break;
  428. /* GTT pagetable sizes bigger than 512KB are not possible on G33! */
  429. case I965_PGETBL_SIZE_1MB:
  430. size = KB(1024);
  431. break;
  432. case I965_PGETBL_SIZE_2MB:
  433. size = KB(2048);
  434. break;
  435. case I965_PGETBL_SIZE_1_5MB:
  436. size = KB(1024 + 512);
  437. break;
  438. default:
  439. dev_info(&intel_private.pcidev->dev,
  440. "unknown page table size, assuming 512KB\n");
  441. size = KB(512);
  442. }
  443. return size/4;
  444. }
  445. static unsigned int intel_gtt_total_entries(void)
  446. {
  447. if (IS_G33 || INTEL_GTT_GEN == 4 || INTEL_GTT_GEN == 5)
  448. return i965_gtt_total_entries();
  449. else {
  450. /* On previous hardware, the GTT size was just what was
  451. * required to map the aperture.
  452. */
  453. return intel_private.gtt_mappable_entries;
  454. }
  455. }
  456. static unsigned int intel_gtt_mappable_entries(void)
  457. {
  458. unsigned int aperture_size;
  459. if (INTEL_GTT_GEN == 1) {
  460. u32 smram_miscc;
  461. pci_read_config_dword(intel_private.bridge_dev,
  462. I810_SMRAM_MISCC, &smram_miscc);
  463. if ((smram_miscc & I810_GFX_MEM_WIN_SIZE)
  464. == I810_GFX_MEM_WIN_32M)
  465. aperture_size = MB(32);
  466. else
  467. aperture_size = MB(64);
  468. } else if (INTEL_GTT_GEN == 2) {
  469. u16 gmch_ctrl;
  470. pci_read_config_word(intel_private.bridge_dev,
  471. I830_GMCH_CTRL, &gmch_ctrl);
  472. if ((gmch_ctrl & I830_GMCH_MEM_MASK) == I830_GMCH_MEM_64M)
  473. aperture_size = MB(64);
  474. else
  475. aperture_size = MB(128);
  476. } else {
  477. /* 9xx supports large sizes, just look at the length */
  478. aperture_size = pci_resource_len(intel_private.pcidev, 2);
  479. }
  480. return aperture_size >> PAGE_SHIFT;
  481. }
  482. static void intel_gtt_teardown_scratch_page(void)
  483. {
  484. set_pages_wb(intel_private.scratch_page, 1);
  485. pci_unmap_page(intel_private.pcidev, intel_private.scratch_page_dma,
  486. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  487. put_page(intel_private.scratch_page);
  488. __free_page(intel_private.scratch_page);
  489. }
  490. static void intel_gtt_cleanup(void)
  491. {
  492. intel_private.driver->cleanup();
  493. iounmap(intel_private.gtt);
  494. iounmap(intel_private.registers);
  495. intel_gtt_teardown_scratch_page();
  496. }
  497. /* Certain Gen5 chipsets require require idling the GPU before
  498. * unmapping anything from the GTT when VT-d is enabled.
  499. */
  500. static inline int needs_ilk_vtd_wa(void)
  501. {
  502. #ifdef CONFIG_INTEL_IOMMU
  503. const unsigned short gpu_devid = intel_private.pcidev->device;
  504. /* Query intel_iommu to see if we need the workaround. Presumably that
  505. * was loaded first.
  506. */
  507. if ((gpu_devid == PCI_DEVICE_ID_INTEL_IRONLAKE_M_HB ||
  508. gpu_devid == PCI_DEVICE_ID_INTEL_IRONLAKE_M_IG) &&
  509. intel_iommu_gfx_mapped)
  510. return 1;
  511. #endif
  512. return 0;
  513. }
  514. static bool intel_gtt_can_wc(void)
  515. {
  516. if (INTEL_GTT_GEN <= 2)
  517. return false;
  518. if (INTEL_GTT_GEN >= 6)
  519. return false;
  520. /* Reports of major corruption with ILK vt'd enabled */
  521. if (needs_ilk_vtd_wa())
  522. return false;
  523. return true;
  524. }
  525. static int intel_gtt_init(void)
  526. {
  527. u32 gtt_map_size;
  528. int ret, bar;
  529. ret = intel_private.driver->setup();
  530. if (ret != 0)
  531. return ret;
  532. intel_private.gtt_mappable_entries = intel_gtt_mappable_entries();
  533. intel_private.gtt_total_entries = intel_gtt_total_entries();
  534. /* save the PGETBL reg for resume */
  535. intel_private.PGETBL_save =
  536. readl(intel_private.registers+I810_PGETBL_CTL)
  537. & ~I810_PGETBL_ENABLED;
  538. /* we only ever restore the register when enabling the PGTBL... */
  539. if (HAS_PGTBL_EN)
  540. intel_private.PGETBL_save |= I810_PGETBL_ENABLED;
  541. dev_info(&intel_private.bridge_dev->dev,
  542. "detected gtt size: %dK total, %dK mappable\n",
  543. intel_private.gtt_total_entries * 4,
  544. intel_private.gtt_mappable_entries * 4);
  545. gtt_map_size = intel_private.gtt_total_entries * 4;
  546. intel_private.gtt = NULL;
  547. if (intel_gtt_can_wc())
  548. intel_private.gtt = ioremap_wc(intel_private.gtt_phys_addr,
  549. gtt_map_size);
  550. if (intel_private.gtt == NULL)
  551. intel_private.gtt = ioremap(intel_private.gtt_phys_addr,
  552. gtt_map_size);
  553. if (intel_private.gtt == NULL) {
  554. intel_private.driver->cleanup();
  555. iounmap(intel_private.registers);
  556. return -ENOMEM;
  557. }
  558. #if IS_ENABLED(CONFIG_AGP_INTEL)
  559. global_cache_flush(); /* FIXME: ? */
  560. #endif
  561. intel_private.stolen_size = intel_gtt_stolen_size();
  562. intel_private.needs_dmar = USE_PCI_DMA_API && INTEL_GTT_GEN > 2;
  563. ret = intel_gtt_setup_scratch_page();
  564. if (ret != 0) {
  565. intel_gtt_cleanup();
  566. return ret;
  567. }
  568. if (INTEL_GTT_GEN <= 2)
  569. bar = I810_GMADR_BAR;
  570. else
  571. bar = I915_GMADR_BAR;
  572. intel_private.gma_bus_addr = pci_bus_address(intel_private.pcidev, bar);
  573. return 0;
  574. }
  575. #if IS_ENABLED(CONFIG_AGP_INTEL)
  576. static int intel_fake_agp_fetch_size(void)
  577. {
  578. int num_sizes = ARRAY_SIZE(intel_fake_agp_sizes);
  579. unsigned int aper_size;
  580. int i;
  581. aper_size = (intel_private.gtt_mappable_entries << PAGE_SHIFT) / MB(1);
  582. for (i = 0; i < num_sizes; i++) {
  583. if (aper_size == intel_fake_agp_sizes[i].size) {
  584. agp_bridge->current_size =
  585. (void *) (intel_fake_agp_sizes + i);
  586. return aper_size;
  587. }
  588. }
  589. return 0;
  590. }
  591. #endif
  592. static void i830_cleanup(void)
  593. {
  594. }
  595. /* The chipset_flush interface needs to get data that has already been
  596. * flushed out of the CPU all the way out to main memory, because the GPU
  597. * doesn't snoop those buffers.
  598. *
  599. * The 8xx series doesn't have the same lovely interface for flushing the
  600. * chipset write buffers that the later chips do. According to the 865
  601. * specs, it's 64 octwords, or 1KB. So, to get those previous things in
  602. * that buffer out, we just fill 1KB and clflush it out, on the assumption
  603. * that it'll push whatever was in there out. It appears to work.
  604. */
  605. static void i830_chipset_flush(void)
  606. {
  607. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  608. /* Forcibly evict everything from the CPU write buffers.
  609. * clflush appears to be insufficient.
  610. */
  611. wbinvd_on_all_cpus();
  612. /* Now we've only seen documents for this magic bit on 855GM,
  613. * we hope it exists for the other gen2 chipsets...
  614. *
  615. * Also works as advertised on my 845G.
  616. */
  617. writel(readl(intel_private.registers+I830_HIC) | (1<<31),
  618. intel_private.registers+I830_HIC);
  619. while (readl(intel_private.registers+I830_HIC) & (1<<31)) {
  620. if (time_after(jiffies, timeout))
  621. break;
  622. udelay(50);
  623. }
  624. }
  625. static void i830_write_entry(dma_addr_t addr, unsigned int entry,
  626. unsigned int flags)
  627. {
  628. u32 pte_flags = I810_PTE_VALID;
  629. if (flags == AGP_USER_CACHED_MEMORY)
  630. pte_flags |= I830_PTE_SYSTEM_CACHED;
  631. writel(addr | pte_flags, intel_private.gtt + entry);
  632. }
  633. bool intel_enable_gtt(void)
  634. {
  635. u8 __iomem *reg;
  636. if (INTEL_GTT_GEN == 2) {
  637. u16 gmch_ctrl;
  638. pci_read_config_word(intel_private.bridge_dev,
  639. I830_GMCH_CTRL, &gmch_ctrl);
  640. gmch_ctrl |= I830_GMCH_ENABLED;
  641. pci_write_config_word(intel_private.bridge_dev,
  642. I830_GMCH_CTRL, gmch_ctrl);
  643. pci_read_config_word(intel_private.bridge_dev,
  644. I830_GMCH_CTRL, &gmch_ctrl);
  645. if ((gmch_ctrl & I830_GMCH_ENABLED) == 0) {
  646. dev_err(&intel_private.pcidev->dev,
  647. "failed to enable the GTT: GMCH_CTRL=%x\n",
  648. gmch_ctrl);
  649. return false;
  650. }
  651. }
  652. /* On the resume path we may be adjusting the PGTBL value, so
  653. * be paranoid and flush all chipset write buffers...
  654. */
  655. if (INTEL_GTT_GEN >= 3)
  656. writel(0, intel_private.registers+GFX_FLSH_CNTL);
  657. reg = intel_private.registers+I810_PGETBL_CTL;
  658. writel(intel_private.PGETBL_save, reg);
  659. if (HAS_PGTBL_EN && (readl(reg) & I810_PGETBL_ENABLED) == 0) {
  660. dev_err(&intel_private.pcidev->dev,
  661. "failed to enable the GTT: PGETBL=%x [expected %x]\n",
  662. readl(reg), intel_private.PGETBL_save);
  663. return false;
  664. }
  665. if (INTEL_GTT_GEN >= 3)
  666. writel(0, intel_private.registers+GFX_FLSH_CNTL);
  667. return true;
  668. }
  669. EXPORT_SYMBOL(intel_enable_gtt);
  670. static int i830_setup(void)
  671. {
  672. phys_addr_t reg_addr;
  673. reg_addr = pci_resource_start(intel_private.pcidev, I810_MMADR_BAR);
  674. intel_private.registers = ioremap(reg_addr, KB(64));
  675. if (!intel_private.registers)
  676. return -ENOMEM;
  677. intel_private.gtt_phys_addr = reg_addr + I810_PTE_BASE;
  678. return 0;
  679. }
  680. #if IS_ENABLED(CONFIG_AGP_INTEL)
  681. static int intel_fake_agp_create_gatt_table(struct agp_bridge_data *bridge)
  682. {
  683. agp_bridge->gatt_table_real = NULL;
  684. agp_bridge->gatt_table = NULL;
  685. agp_bridge->gatt_bus_addr = 0;
  686. return 0;
  687. }
  688. static int intel_fake_agp_free_gatt_table(struct agp_bridge_data *bridge)
  689. {
  690. return 0;
  691. }
  692. static int intel_fake_agp_configure(void)
  693. {
  694. if (!intel_enable_gtt())
  695. return -EIO;
  696. intel_private.clear_fake_agp = true;
  697. agp_bridge->gart_bus_addr = intel_private.gma_bus_addr;
  698. return 0;
  699. }
  700. #endif
  701. static bool i830_check_flags(unsigned int flags)
  702. {
  703. switch (flags) {
  704. case 0:
  705. case AGP_PHYS_MEMORY:
  706. case AGP_USER_CACHED_MEMORY:
  707. case AGP_USER_MEMORY:
  708. return true;
  709. }
  710. return false;
  711. }
  712. void intel_gtt_insert_sg_entries(struct sg_table *st,
  713. unsigned int pg_start,
  714. unsigned int flags)
  715. {
  716. struct scatterlist *sg;
  717. unsigned int len, m;
  718. int i, j;
  719. j = pg_start;
  720. /* sg may merge pages, but we have to separate
  721. * per-page addr for GTT */
  722. for_each_sg(st->sgl, sg, st->nents, i) {
  723. len = sg_dma_len(sg) >> PAGE_SHIFT;
  724. for (m = 0; m < len; m++) {
  725. dma_addr_t addr = sg_dma_address(sg) + (m << PAGE_SHIFT);
  726. intel_private.driver->write_entry(addr, j, flags);
  727. j++;
  728. }
  729. }
  730. readl(intel_private.gtt+j-1);
  731. }
  732. EXPORT_SYMBOL(intel_gtt_insert_sg_entries);
  733. #if IS_ENABLED(CONFIG_AGP_INTEL)
  734. static void intel_gtt_insert_pages(unsigned int first_entry,
  735. unsigned int num_entries,
  736. struct page **pages,
  737. unsigned int flags)
  738. {
  739. int i, j;
  740. for (i = 0, j = first_entry; i < num_entries; i++, j++) {
  741. dma_addr_t addr = page_to_phys(pages[i]);
  742. intel_private.driver->write_entry(addr,
  743. j, flags);
  744. }
  745. readl(intel_private.gtt+j-1);
  746. }
  747. static int intel_fake_agp_insert_entries(struct agp_memory *mem,
  748. off_t pg_start, int type)
  749. {
  750. int ret = -EINVAL;
  751. if (intel_private.clear_fake_agp) {
  752. int start = intel_private.stolen_size / PAGE_SIZE;
  753. int end = intel_private.gtt_mappable_entries;
  754. intel_gtt_clear_range(start, end - start);
  755. intel_private.clear_fake_agp = false;
  756. }
  757. if (INTEL_GTT_GEN == 1 && type == AGP_DCACHE_MEMORY)
  758. return i810_insert_dcache_entries(mem, pg_start, type);
  759. if (mem->page_count == 0)
  760. goto out;
  761. if (pg_start + mem->page_count > intel_private.gtt_total_entries)
  762. goto out_err;
  763. if (type != mem->type)
  764. goto out_err;
  765. if (!intel_private.driver->check_flags(type))
  766. goto out_err;
  767. if (!mem->is_flushed)
  768. global_cache_flush();
  769. if (intel_private.needs_dmar) {
  770. struct sg_table st;
  771. ret = intel_gtt_map_memory(mem->pages, mem->page_count, &st);
  772. if (ret != 0)
  773. return ret;
  774. intel_gtt_insert_sg_entries(&st, pg_start, type);
  775. mem->sg_list = st.sgl;
  776. mem->num_sg = st.nents;
  777. } else
  778. intel_gtt_insert_pages(pg_start, mem->page_count, mem->pages,
  779. type);
  780. out:
  781. ret = 0;
  782. out_err:
  783. mem->is_flushed = true;
  784. return ret;
  785. }
  786. #endif
  787. void intel_gtt_clear_range(unsigned int first_entry, unsigned int num_entries)
  788. {
  789. unsigned int i;
  790. for (i = first_entry; i < (first_entry + num_entries); i++) {
  791. intel_private.driver->write_entry(intel_private.scratch_page_dma,
  792. i, 0);
  793. }
  794. readl(intel_private.gtt+i-1);
  795. }
  796. EXPORT_SYMBOL(intel_gtt_clear_range);
  797. #if IS_ENABLED(CONFIG_AGP_INTEL)
  798. static int intel_fake_agp_remove_entries(struct agp_memory *mem,
  799. off_t pg_start, int type)
  800. {
  801. if (mem->page_count == 0)
  802. return 0;
  803. intel_gtt_clear_range(pg_start, mem->page_count);
  804. if (intel_private.needs_dmar) {
  805. intel_gtt_unmap_memory(mem->sg_list, mem->num_sg);
  806. mem->sg_list = NULL;
  807. mem->num_sg = 0;
  808. }
  809. return 0;
  810. }
  811. static struct agp_memory *intel_fake_agp_alloc_by_type(size_t pg_count,
  812. int type)
  813. {
  814. struct agp_memory *new;
  815. if (type == AGP_DCACHE_MEMORY && INTEL_GTT_GEN == 1) {
  816. if (pg_count != intel_private.num_dcache_entries)
  817. return NULL;
  818. new = agp_create_memory(1);
  819. if (new == NULL)
  820. return NULL;
  821. new->type = AGP_DCACHE_MEMORY;
  822. new->page_count = pg_count;
  823. new->num_scratch_pages = 0;
  824. agp_free_page_array(new);
  825. return new;
  826. }
  827. if (type == AGP_PHYS_MEMORY)
  828. return alloc_agpphysmem_i8xx(pg_count, type);
  829. /* always return NULL for other allocation types for now */
  830. return NULL;
  831. }
  832. #endif
  833. static int intel_alloc_chipset_flush_resource(void)
  834. {
  835. int ret;
  836. ret = pci_bus_alloc_resource(intel_private.bridge_dev->bus, &intel_private.ifp_resource, PAGE_SIZE,
  837. PAGE_SIZE, PCIBIOS_MIN_MEM, 0,
  838. pcibios_align_resource, intel_private.bridge_dev);
  839. return ret;
  840. }
  841. static void intel_i915_setup_chipset_flush(void)
  842. {
  843. int ret;
  844. u32 temp;
  845. pci_read_config_dword(intel_private.bridge_dev, I915_IFPADDR, &temp);
  846. if (!(temp & 0x1)) {
  847. intel_alloc_chipset_flush_resource();
  848. intel_private.resource_valid = 1;
  849. pci_write_config_dword(intel_private.bridge_dev, I915_IFPADDR, (intel_private.ifp_resource.start & 0xffffffff) | 0x1);
  850. } else {
  851. temp &= ~1;
  852. intel_private.resource_valid = 1;
  853. intel_private.ifp_resource.start = temp;
  854. intel_private.ifp_resource.end = temp + PAGE_SIZE;
  855. ret = request_resource(&iomem_resource, &intel_private.ifp_resource);
  856. /* some BIOSes reserve this area in a pnp some don't */
  857. if (ret)
  858. intel_private.resource_valid = 0;
  859. }
  860. }
  861. static void intel_i965_g33_setup_chipset_flush(void)
  862. {
  863. u32 temp_hi, temp_lo;
  864. int ret;
  865. pci_read_config_dword(intel_private.bridge_dev, I965_IFPADDR + 4, &temp_hi);
  866. pci_read_config_dword(intel_private.bridge_dev, I965_IFPADDR, &temp_lo);
  867. if (!(temp_lo & 0x1)) {
  868. intel_alloc_chipset_flush_resource();
  869. intel_private.resource_valid = 1;
  870. pci_write_config_dword(intel_private.bridge_dev, I965_IFPADDR + 4,
  871. upper_32_bits(intel_private.ifp_resource.start));
  872. pci_write_config_dword(intel_private.bridge_dev, I965_IFPADDR, (intel_private.ifp_resource.start & 0xffffffff) | 0x1);
  873. } else {
  874. u64 l64;
  875. temp_lo &= ~0x1;
  876. l64 = ((u64)temp_hi << 32) | temp_lo;
  877. intel_private.resource_valid = 1;
  878. intel_private.ifp_resource.start = l64;
  879. intel_private.ifp_resource.end = l64 + PAGE_SIZE;
  880. ret = request_resource(&iomem_resource, &intel_private.ifp_resource);
  881. /* some BIOSes reserve this area in a pnp some don't */
  882. if (ret)
  883. intel_private.resource_valid = 0;
  884. }
  885. }
  886. static void intel_i9xx_setup_flush(void)
  887. {
  888. /* return if already configured */
  889. if (intel_private.ifp_resource.start)
  890. return;
  891. if (INTEL_GTT_GEN == 6)
  892. return;
  893. /* setup a resource for this object */
  894. intel_private.ifp_resource.name = "Intel Flush Page";
  895. intel_private.ifp_resource.flags = IORESOURCE_MEM;
  896. /* Setup chipset flush for 915 */
  897. if (IS_G33 || INTEL_GTT_GEN >= 4) {
  898. intel_i965_g33_setup_chipset_flush();
  899. } else {
  900. intel_i915_setup_chipset_flush();
  901. }
  902. if (intel_private.ifp_resource.start)
  903. intel_private.i9xx_flush_page = ioremap_nocache(intel_private.ifp_resource.start, PAGE_SIZE);
  904. if (!intel_private.i9xx_flush_page)
  905. dev_err(&intel_private.pcidev->dev,
  906. "can't ioremap flush page - no chipset flushing\n");
  907. }
  908. static void i9xx_cleanup(void)
  909. {
  910. if (intel_private.i9xx_flush_page)
  911. iounmap(intel_private.i9xx_flush_page);
  912. if (intel_private.resource_valid)
  913. release_resource(&intel_private.ifp_resource);
  914. intel_private.ifp_resource.start = 0;
  915. intel_private.resource_valid = 0;
  916. }
  917. static void i9xx_chipset_flush(void)
  918. {
  919. if (intel_private.i9xx_flush_page)
  920. writel(1, intel_private.i9xx_flush_page);
  921. }
  922. static void i965_write_entry(dma_addr_t addr,
  923. unsigned int entry,
  924. unsigned int flags)
  925. {
  926. u32 pte_flags;
  927. pte_flags = I810_PTE_VALID;
  928. if (flags == AGP_USER_CACHED_MEMORY)
  929. pte_flags |= I830_PTE_SYSTEM_CACHED;
  930. /* Shift high bits down */
  931. addr |= (addr >> 28) & 0xf0;
  932. writel(addr | pte_flags, intel_private.gtt + entry);
  933. }
  934. static int i9xx_setup(void)
  935. {
  936. phys_addr_t reg_addr;
  937. int size = KB(512);
  938. reg_addr = pci_resource_start(intel_private.pcidev, I915_MMADR_BAR);
  939. intel_private.registers = ioremap(reg_addr, size);
  940. if (!intel_private.registers)
  941. return -ENOMEM;
  942. switch (INTEL_GTT_GEN) {
  943. case 3:
  944. intel_private.gtt_phys_addr =
  945. pci_resource_start(intel_private.pcidev, I915_PTE_BAR);
  946. break;
  947. case 5:
  948. intel_private.gtt_phys_addr = reg_addr + MB(2);
  949. break;
  950. default:
  951. intel_private.gtt_phys_addr = reg_addr + KB(512);
  952. break;
  953. }
  954. intel_i9xx_setup_flush();
  955. return 0;
  956. }
  957. #if IS_ENABLED(CONFIG_AGP_INTEL)
  958. static const struct agp_bridge_driver intel_fake_agp_driver = {
  959. .owner = THIS_MODULE,
  960. .size_type = FIXED_APER_SIZE,
  961. .aperture_sizes = intel_fake_agp_sizes,
  962. .num_aperture_sizes = ARRAY_SIZE(intel_fake_agp_sizes),
  963. .configure = intel_fake_agp_configure,
  964. .fetch_size = intel_fake_agp_fetch_size,
  965. .cleanup = intel_gtt_cleanup,
  966. .agp_enable = intel_fake_agp_enable,
  967. .cache_flush = global_cache_flush,
  968. .create_gatt_table = intel_fake_agp_create_gatt_table,
  969. .free_gatt_table = intel_fake_agp_free_gatt_table,
  970. .insert_memory = intel_fake_agp_insert_entries,
  971. .remove_memory = intel_fake_agp_remove_entries,
  972. .alloc_by_type = intel_fake_agp_alloc_by_type,
  973. .free_by_type = intel_i810_free_by_type,
  974. .agp_alloc_page = agp_generic_alloc_page,
  975. .agp_alloc_pages = agp_generic_alloc_pages,
  976. .agp_destroy_page = agp_generic_destroy_page,
  977. .agp_destroy_pages = agp_generic_destroy_pages,
  978. };
  979. #endif
  980. static const struct intel_gtt_driver i81x_gtt_driver = {
  981. .gen = 1,
  982. .has_pgtbl_enable = 1,
  983. .dma_mask_size = 32,
  984. .setup = i810_setup,
  985. .cleanup = i810_cleanup,
  986. .check_flags = i830_check_flags,
  987. .write_entry = i810_write_entry,
  988. };
  989. static const struct intel_gtt_driver i8xx_gtt_driver = {
  990. .gen = 2,
  991. .has_pgtbl_enable = 1,
  992. .setup = i830_setup,
  993. .cleanup = i830_cleanup,
  994. .write_entry = i830_write_entry,
  995. .dma_mask_size = 32,
  996. .check_flags = i830_check_flags,
  997. .chipset_flush = i830_chipset_flush,
  998. };
  999. static const struct intel_gtt_driver i915_gtt_driver = {
  1000. .gen = 3,
  1001. .has_pgtbl_enable = 1,
  1002. .setup = i9xx_setup,
  1003. .cleanup = i9xx_cleanup,
  1004. /* i945 is the last gpu to need phys mem (for overlay and cursors). */
  1005. .write_entry = i830_write_entry,
  1006. .dma_mask_size = 32,
  1007. .check_flags = i830_check_flags,
  1008. .chipset_flush = i9xx_chipset_flush,
  1009. };
  1010. static const struct intel_gtt_driver g33_gtt_driver = {
  1011. .gen = 3,
  1012. .is_g33 = 1,
  1013. .setup = i9xx_setup,
  1014. .cleanup = i9xx_cleanup,
  1015. .write_entry = i965_write_entry,
  1016. .dma_mask_size = 36,
  1017. .check_flags = i830_check_flags,
  1018. .chipset_flush = i9xx_chipset_flush,
  1019. };
  1020. static const struct intel_gtt_driver pineview_gtt_driver = {
  1021. .gen = 3,
  1022. .is_pineview = 1, .is_g33 = 1,
  1023. .setup = i9xx_setup,
  1024. .cleanup = i9xx_cleanup,
  1025. .write_entry = i965_write_entry,
  1026. .dma_mask_size = 36,
  1027. .check_flags = i830_check_flags,
  1028. .chipset_flush = i9xx_chipset_flush,
  1029. };
  1030. static const struct intel_gtt_driver i965_gtt_driver = {
  1031. .gen = 4,
  1032. .has_pgtbl_enable = 1,
  1033. .setup = i9xx_setup,
  1034. .cleanup = i9xx_cleanup,
  1035. .write_entry = i965_write_entry,
  1036. .dma_mask_size = 36,
  1037. .check_flags = i830_check_flags,
  1038. .chipset_flush = i9xx_chipset_flush,
  1039. };
  1040. static const struct intel_gtt_driver g4x_gtt_driver = {
  1041. .gen = 5,
  1042. .setup = i9xx_setup,
  1043. .cleanup = i9xx_cleanup,
  1044. .write_entry = i965_write_entry,
  1045. .dma_mask_size = 36,
  1046. .check_flags = i830_check_flags,
  1047. .chipset_flush = i9xx_chipset_flush,
  1048. };
  1049. static const struct intel_gtt_driver ironlake_gtt_driver = {
  1050. .gen = 5,
  1051. .is_ironlake = 1,
  1052. .setup = i9xx_setup,
  1053. .cleanup = i9xx_cleanup,
  1054. .write_entry = i965_write_entry,
  1055. .dma_mask_size = 36,
  1056. .check_flags = i830_check_flags,
  1057. .chipset_flush = i9xx_chipset_flush,
  1058. };
  1059. /* Table to describe Intel GMCH and AGP/PCIE GART drivers. At least one of
  1060. * driver and gmch_driver must be non-null, and find_gmch will determine
  1061. * which one should be used if a gmch_chip_id is present.
  1062. */
  1063. static const struct intel_gtt_driver_description {
  1064. unsigned int gmch_chip_id;
  1065. char *name;
  1066. const struct intel_gtt_driver *gtt_driver;
  1067. } intel_gtt_chipsets[] = {
  1068. { PCI_DEVICE_ID_INTEL_82810_IG1, "i810",
  1069. &i81x_gtt_driver},
  1070. { PCI_DEVICE_ID_INTEL_82810_IG3, "i810",
  1071. &i81x_gtt_driver},
  1072. { PCI_DEVICE_ID_INTEL_82810E_IG, "i810",
  1073. &i81x_gtt_driver},
  1074. { PCI_DEVICE_ID_INTEL_82815_CGC, "i815",
  1075. &i81x_gtt_driver},
  1076. { PCI_DEVICE_ID_INTEL_82830_CGC, "830M",
  1077. &i8xx_gtt_driver},
  1078. { PCI_DEVICE_ID_INTEL_82845G_IG, "845G",
  1079. &i8xx_gtt_driver},
  1080. { PCI_DEVICE_ID_INTEL_82854_IG, "854",
  1081. &i8xx_gtt_driver},
  1082. { PCI_DEVICE_ID_INTEL_82855GM_IG, "855GM",
  1083. &i8xx_gtt_driver},
  1084. { PCI_DEVICE_ID_INTEL_82865_IG, "865",
  1085. &i8xx_gtt_driver},
  1086. { PCI_DEVICE_ID_INTEL_E7221_IG, "E7221 (i915)",
  1087. &i915_gtt_driver },
  1088. { PCI_DEVICE_ID_INTEL_82915G_IG, "915G",
  1089. &i915_gtt_driver },
  1090. { PCI_DEVICE_ID_INTEL_82915GM_IG, "915GM",
  1091. &i915_gtt_driver },
  1092. { PCI_DEVICE_ID_INTEL_82945G_IG, "945G",
  1093. &i915_gtt_driver },
  1094. { PCI_DEVICE_ID_INTEL_82945GM_IG, "945GM",
  1095. &i915_gtt_driver },
  1096. { PCI_DEVICE_ID_INTEL_82945GME_IG, "945GME",
  1097. &i915_gtt_driver },
  1098. { PCI_DEVICE_ID_INTEL_82946GZ_IG, "946GZ",
  1099. &i965_gtt_driver },
  1100. { PCI_DEVICE_ID_INTEL_82G35_IG, "G35",
  1101. &i965_gtt_driver },
  1102. { PCI_DEVICE_ID_INTEL_82965Q_IG, "965Q",
  1103. &i965_gtt_driver },
  1104. { PCI_DEVICE_ID_INTEL_82965G_IG, "965G",
  1105. &i965_gtt_driver },
  1106. { PCI_DEVICE_ID_INTEL_82965GM_IG, "965GM",
  1107. &i965_gtt_driver },
  1108. { PCI_DEVICE_ID_INTEL_82965GME_IG, "965GME/GLE",
  1109. &i965_gtt_driver },
  1110. { PCI_DEVICE_ID_INTEL_G33_IG, "G33",
  1111. &g33_gtt_driver },
  1112. { PCI_DEVICE_ID_INTEL_Q35_IG, "Q35",
  1113. &g33_gtt_driver },
  1114. { PCI_DEVICE_ID_INTEL_Q33_IG, "Q33",
  1115. &g33_gtt_driver },
  1116. { PCI_DEVICE_ID_INTEL_PINEVIEW_M_IG, "GMA3150",
  1117. &pineview_gtt_driver },
  1118. { PCI_DEVICE_ID_INTEL_PINEVIEW_IG, "GMA3150",
  1119. &pineview_gtt_driver },
  1120. { PCI_DEVICE_ID_INTEL_GM45_IG, "GM45",
  1121. &g4x_gtt_driver },
  1122. { PCI_DEVICE_ID_INTEL_EAGLELAKE_IG, "Eaglelake",
  1123. &g4x_gtt_driver },
  1124. { PCI_DEVICE_ID_INTEL_Q45_IG, "Q45/Q43",
  1125. &g4x_gtt_driver },
  1126. { PCI_DEVICE_ID_INTEL_G45_IG, "G45/G43",
  1127. &g4x_gtt_driver },
  1128. { PCI_DEVICE_ID_INTEL_B43_IG, "B43",
  1129. &g4x_gtt_driver },
  1130. { PCI_DEVICE_ID_INTEL_B43_1_IG, "B43",
  1131. &g4x_gtt_driver },
  1132. { PCI_DEVICE_ID_INTEL_G41_IG, "G41",
  1133. &g4x_gtt_driver },
  1134. { PCI_DEVICE_ID_INTEL_IRONLAKE_D_IG,
  1135. "HD Graphics", &ironlake_gtt_driver },
  1136. { PCI_DEVICE_ID_INTEL_IRONLAKE_M_IG,
  1137. "HD Graphics", &ironlake_gtt_driver },
  1138. { 0, NULL, NULL }
  1139. };
  1140. static int find_gmch(u16 device)
  1141. {
  1142. struct pci_dev *gmch_device;
  1143. gmch_device = pci_get_device(PCI_VENDOR_ID_INTEL, device, NULL);
  1144. if (gmch_device && PCI_FUNC(gmch_device->devfn) != 0) {
  1145. gmch_device = pci_get_device(PCI_VENDOR_ID_INTEL,
  1146. device, gmch_device);
  1147. }
  1148. if (!gmch_device)
  1149. return 0;
  1150. intel_private.pcidev = gmch_device;
  1151. return 1;
  1152. }
  1153. int intel_gmch_probe(struct pci_dev *bridge_pdev, struct pci_dev *gpu_pdev,
  1154. struct agp_bridge_data *bridge)
  1155. {
  1156. int i, mask;
  1157. /*
  1158. * Can be called from the fake agp driver but also directly from
  1159. * drm/i915.ko. Hence we need to check whether everything is set up
  1160. * already.
  1161. */
  1162. if (intel_private.driver) {
  1163. intel_private.refcount++;
  1164. return 1;
  1165. }
  1166. for (i = 0; intel_gtt_chipsets[i].name != NULL; i++) {
  1167. if (gpu_pdev) {
  1168. if (gpu_pdev->device ==
  1169. intel_gtt_chipsets[i].gmch_chip_id) {
  1170. intel_private.pcidev = pci_dev_get(gpu_pdev);
  1171. intel_private.driver =
  1172. intel_gtt_chipsets[i].gtt_driver;
  1173. break;
  1174. }
  1175. } else if (find_gmch(intel_gtt_chipsets[i].gmch_chip_id)) {
  1176. intel_private.driver =
  1177. intel_gtt_chipsets[i].gtt_driver;
  1178. break;
  1179. }
  1180. }
  1181. if (!intel_private.driver)
  1182. return 0;
  1183. intel_private.refcount++;
  1184. #if IS_ENABLED(CONFIG_AGP_INTEL)
  1185. if (bridge) {
  1186. bridge->driver = &intel_fake_agp_driver;
  1187. bridge->dev_private_data = &intel_private;
  1188. bridge->dev = bridge_pdev;
  1189. }
  1190. #endif
  1191. intel_private.bridge_dev = pci_dev_get(bridge_pdev);
  1192. dev_info(&bridge_pdev->dev, "Intel %s Chipset\n", intel_gtt_chipsets[i].name);
  1193. mask = intel_private.driver->dma_mask_size;
  1194. if (pci_set_dma_mask(intel_private.pcidev, DMA_BIT_MASK(mask)))
  1195. dev_err(&intel_private.pcidev->dev,
  1196. "set gfx device dma mask %d-bit failed!\n", mask);
  1197. else
  1198. pci_set_consistent_dma_mask(intel_private.pcidev,
  1199. DMA_BIT_MASK(mask));
  1200. if (intel_gtt_init() != 0) {
  1201. intel_gmch_remove();
  1202. return 0;
  1203. }
  1204. return 1;
  1205. }
  1206. EXPORT_SYMBOL(intel_gmch_probe);
  1207. void intel_gtt_get(size_t *gtt_total, size_t *stolen_size,
  1208. phys_addr_t *mappable_base, unsigned long *mappable_end)
  1209. {
  1210. *gtt_total = intel_private.gtt_total_entries << PAGE_SHIFT;
  1211. *stolen_size = intel_private.stolen_size;
  1212. *mappable_base = intel_private.gma_bus_addr;
  1213. *mappable_end = intel_private.gtt_mappable_entries << PAGE_SHIFT;
  1214. }
  1215. EXPORT_SYMBOL(intel_gtt_get);
  1216. void intel_gtt_chipset_flush(void)
  1217. {
  1218. if (intel_private.driver->chipset_flush)
  1219. intel_private.driver->chipset_flush();
  1220. }
  1221. EXPORT_SYMBOL(intel_gtt_chipset_flush);
  1222. void intel_gmch_remove(void)
  1223. {
  1224. if (--intel_private.refcount)
  1225. return;
  1226. if (intel_private.pcidev)
  1227. pci_dev_put(intel_private.pcidev);
  1228. if (intel_private.bridge_dev)
  1229. pci_dev_put(intel_private.bridge_dev);
  1230. intel_private.driver = NULL;
  1231. }
  1232. EXPORT_SYMBOL(intel_gmch_remove);
  1233. MODULE_AUTHOR("Dave Jones <davej@redhat.com>");
  1234. MODULE_LICENSE("GPL and additional rights");