intel-gtt.c 36 KB

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