sworks-agp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*
  2. * Serverworks AGPGART routines.
  3. */
  4. #include <linux/module.h>
  5. #include <linux/pci.h>
  6. #include <linux/init.h>
  7. #include <linux/string.h>
  8. #include <linux/slab.h>
  9. #include <linux/jiffies.h>
  10. #include <linux/agp_backend.h>
  11. #include <asm/set_memory.h>
  12. #include "agp.h"
  13. #define SVWRKS_COMMAND 0x04
  14. #define SVWRKS_APSIZE 0x10
  15. #define SVWRKS_MMBASE 0x14
  16. #define SVWRKS_CACHING 0x4b
  17. #define SVWRKS_AGP_ENABLE 0x60
  18. #define SVWRKS_FEATURE 0x68
  19. #define SVWRKS_SIZE_MASK 0xfe000000
  20. /* Memory mapped registers */
  21. #define SVWRKS_GART_CACHE 0x02
  22. #define SVWRKS_GATTBASE 0x04
  23. #define SVWRKS_TLBFLUSH 0x10
  24. #define SVWRKS_POSTFLUSH 0x14
  25. #define SVWRKS_DIRFLUSH 0x0c
  26. struct serverworks_page_map {
  27. unsigned long *real;
  28. unsigned long __iomem *remapped;
  29. };
  30. static struct _serverworks_private {
  31. struct pci_dev *svrwrks_dev; /* device one */
  32. volatile u8 __iomem *registers;
  33. struct serverworks_page_map **gatt_pages;
  34. int num_tables;
  35. struct serverworks_page_map scratch_dir;
  36. int gart_addr_ofs;
  37. int mm_addr_ofs;
  38. } serverworks_private;
  39. static int serverworks_create_page_map(struct serverworks_page_map *page_map)
  40. {
  41. int i;
  42. page_map->real = (unsigned long *) __get_free_page(GFP_KERNEL);
  43. if (page_map->real == NULL) {
  44. return -ENOMEM;
  45. }
  46. set_memory_uc((unsigned long)page_map->real, 1);
  47. page_map->remapped = page_map->real;
  48. for (i = 0; i < PAGE_SIZE / sizeof(unsigned long); i++)
  49. writel(agp_bridge->scratch_page, page_map->remapped+i);
  50. /* Red Pen: Everyone else does pci posting flush here */
  51. return 0;
  52. }
  53. static void serverworks_free_page_map(struct serverworks_page_map *page_map)
  54. {
  55. set_memory_wb((unsigned long)page_map->real, 1);
  56. free_page((unsigned long) page_map->real);
  57. }
  58. static void serverworks_free_gatt_pages(void)
  59. {
  60. int i;
  61. struct serverworks_page_map **tables;
  62. struct serverworks_page_map *entry;
  63. tables = serverworks_private.gatt_pages;
  64. for (i = 0; i < serverworks_private.num_tables; i++) {
  65. entry = tables[i];
  66. if (entry != NULL) {
  67. if (entry->real != NULL) {
  68. serverworks_free_page_map(entry);
  69. }
  70. kfree(entry);
  71. }
  72. }
  73. kfree(tables);
  74. }
  75. static int serverworks_create_gatt_pages(int nr_tables)
  76. {
  77. struct serverworks_page_map **tables;
  78. struct serverworks_page_map *entry;
  79. int retval = 0;
  80. int i;
  81. tables = kcalloc(nr_tables + 1, sizeof(struct serverworks_page_map *),
  82. GFP_KERNEL);
  83. if (tables == NULL)
  84. return -ENOMEM;
  85. for (i = 0; i < nr_tables; i++) {
  86. entry = kzalloc(sizeof(struct serverworks_page_map), GFP_KERNEL);
  87. if (entry == NULL) {
  88. retval = -ENOMEM;
  89. break;
  90. }
  91. tables[i] = entry;
  92. retval = serverworks_create_page_map(entry);
  93. if (retval != 0) break;
  94. }
  95. serverworks_private.num_tables = nr_tables;
  96. serverworks_private.gatt_pages = tables;
  97. if (retval != 0) serverworks_free_gatt_pages();
  98. return retval;
  99. }
  100. #define SVRWRKS_GET_GATT(addr) (serverworks_private.gatt_pages[\
  101. GET_PAGE_DIR_IDX(addr)]->remapped)
  102. #ifndef GET_PAGE_DIR_OFF
  103. #define GET_PAGE_DIR_OFF(addr) (addr >> 22)
  104. #endif
  105. #ifndef GET_PAGE_DIR_IDX
  106. #define GET_PAGE_DIR_IDX(addr) (GET_PAGE_DIR_OFF(addr) - \
  107. GET_PAGE_DIR_OFF(agp_bridge->gart_bus_addr))
  108. #endif
  109. #ifndef GET_GATT_OFF
  110. #define GET_GATT_OFF(addr) ((addr & 0x003ff000) >> 12)
  111. #endif
  112. static int serverworks_create_gatt_table(struct agp_bridge_data *bridge)
  113. {
  114. struct aper_size_info_lvl2 *value;
  115. struct serverworks_page_map page_dir;
  116. int retval;
  117. u32 temp;
  118. int i;
  119. value = A_SIZE_LVL2(agp_bridge->current_size);
  120. retval = serverworks_create_page_map(&page_dir);
  121. if (retval != 0) {
  122. return retval;
  123. }
  124. retval = serverworks_create_page_map(&serverworks_private.scratch_dir);
  125. if (retval != 0) {
  126. serverworks_free_page_map(&page_dir);
  127. return retval;
  128. }
  129. /* Create a fake scratch directory */
  130. for (i = 0; i < 1024; i++) {
  131. writel(agp_bridge->scratch_page, serverworks_private.scratch_dir.remapped+i);
  132. writel(virt_to_phys(serverworks_private.scratch_dir.real) | 1, page_dir.remapped+i);
  133. }
  134. retval = serverworks_create_gatt_pages(value->num_entries / 1024);
  135. if (retval != 0) {
  136. serverworks_free_page_map(&page_dir);
  137. serverworks_free_page_map(&serverworks_private.scratch_dir);
  138. return retval;
  139. }
  140. agp_bridge->gatt_table_real = (u32 *)page_dir.real;
  141. agp_bridge->gatt_table = (u32 __iomem *)page_dir.remapped;
  142. agp_bridge->gatt_bus_addr = virt_to_phys(page_dir.real);
  143. /* Get the address for the gart region.
  144. * This is a bus address even on the alpha, b/c its
  145. * used to program the agp master not the cpu
  146. */
  147. pci_read_config_dword(agp_bridge->dev,serverworks_private.gart_addr_ofs,&temp);
  148. agp_bridge->gart_bus_addr = (temp & PCI_BASE_ADDRESS_MEM_MASK);
  149. /* Calculate the agp offset */
  150. for (i = 0; i < value->num_entries / 1024; i++)
  151. writel(virt_to_phys(serverworks_private.gatt_pages[i]->real)|1, page_dir.remapped+i);
  152. return 0;
  153. }
  154. static int serverworks_free_gatt_table(struct agp_bridge_data *bridge)
  155. {
  156. struct serverworks_page_map page_dir;
  157. page_dir.real = (unsigned long *)agp_bridge->gatt_table_real;
  158. page_dir.remapped = (unsigned long __iomem *)agp_bridge->gatt_table;
  159. serverworks_free_gatt_pages();
  160. serverworks_free_page_map(&page_dir);
  161. serverworks_free_page_map(&serverworks_private.scratch_dir);
  162. return 0;
  163. }
  164. static int serverworks_fetch_size(void)
  165. {
  166. int i;
  167. u32 temp;
  168. u32 temp2;
  169. struct aper_size_info_lvl2 *values;
  170. values = A_SIZE_LVL2(agp_bridge->driver->aperture_sizes);
  171. pci_read_config_dword(agp_bridge->dev,serverworks_private.gart_addr_ofs,&temp);
  172. pci_write_config_dword(agp_bridge->dev,serverworks_private.gart_addr_ofs,
  173. SVWRKS_SIZE_MASK);
  174. pci_read_config_dword(agp_bridge->dev,serverworks_private.gart_addr_ofs,&temp2);
  175. pci_write_config_dword(agp_bridge->dev,serverworks_private.gart_addr_ofs,temp);
  176. temp2 &= SVWRKS_SIZE_MASK;
  177. for (i = 0; i < agp_bridge->driver->num_aperture_sizes; i++) {
  178. if (temp2 == values[i].size_value) {
  179. agp_bridge->previous_size =
  180. agp_bridge->current_size = (void *) (values + i);
  181. agp_bridge->aperture_size_idx = i;
  182. return values[i].size;
  183. }
  184. }
  185. return 0;
  186. }
  187. /*
  188. * This routine could be implemented by taking the addresses
  189. * written to the GATT, and flushing them individually. However
  190. * currently it just flushes the whole table. Which is probably
  191. * more efficient, since agp_memory blocks can be a large number of
  192. * entries.
  193. */
  194. static void serverworks_tlbflush(struct agp_memory *temp)
  195. {
  196. unsigned long timeout;
  197. writeb(1, serverworks_private.registers+SVWRKS_POSTFLUSH);
  198. timeout = jiffies + 3*HZ;
  199. while (readb(serverworks_private.registers+SVWRKS_POSTFLUSH) == 1) {
  200. cpu_relax();
  201. if (time_after(jiffies, timeout)) {
  202. dev_err(&serverworks_private.svrwrks_dev->dev,
  203. "TLB post flush took more than 3 seconds\n");
  204. break;
  205. }
  206. }
  207. writel(1, serverworks_private.registers+SVWRKS_DIRFLUSH);
  208. timeout = jiffies + 3*HZ;
  209. while (readl(serverworks_private.registers+SVWRKS_DIRFLUSH) == 1) {
  210. cpu_relax();
  211. if (time_after(jiffies, timeout)) {
  212. dev_err(&serverworks_private.svrwrks_dev->dev,
  213. "TLB Dir flush took more than 3 seconds\n");
  214. break;
  215. }
  216. }
  217. }
  218. static int serverworks_configure(void)
  219. {
  220. struct aper_size_info_lvl2 *current_size;
  221. u32 temp;
  222. u8 enable_reg;
  223. u16 cap_reg;
  224. current_size = A_SIZE_LVL2(agp_bridge->current_size);
  225. /* Get the memory mapped registers */
  226. pci_read_config_dword(agp_bridge->dev, serverworks_private.mm_addr_ofs, &temp);
  227. temp = (temp & PCI_BASE_ADDRESS_MEM_MASK);
  228. serverworks_private.registers = (volatile u8 __iomem *) ioremap(temp, 4096);
  229. if (!serverworks_private.registers) {
  230. dev_err(&agp_bridge->dev->dev, "can't ioremap(%#x)\n", temp);
  231. return -ENOMEM;
  232. }
  233. writeb(0xA, serverworks_private.registers+SVWRKS_GART_CACHE);
  234. readb(serverworks_private.registers+SVWRKS_GART_CACHE); /* PCI Posting. */
  235. writel(agp_bridge->gatt_bus_addr, serverworks_private.registers+SVWRKS_GATTBASE);
  236. readl(serverworks_private.registers+SVWRKS_GATTBASE); /* PCI Posting. */
  237. cap_reg = readw(serverworks_private.registers+SVWRKS_COMMAND);
  238. cap_reg &= ~0x0007;
  239. cap_reg |= 0x4;
  240. writew(cap_reg, serverworks_private.registers+SVWRKS_COMMAND);
  241. readw(serverworks_private.registers+SVWRKS_COMMAND);
  242. pci_read_config_byte(serverworks_private.svrwrks_dev,SVWRKS_AGP_ENABLE, &enable_reg);
  243. enable_reg |= 0x1; /* Agp Enable bit */
  244. pci_write_config_byte(serverworks_private.svrwrks_dev,SVWRKS_AGP_ENABLE, enable_reg);
  245. serverworks_tlbflush(NULL);
  246. agp_bridge->capndx = pci_find_capability(serverworks_private.svrwrks_dev, PCI_CAP_ID_AGP);
  247. /* Fill in the mode register */
  248. pci_read_config_dword(serverworks_private.svrwrks_dev,
  249. agp_bridge->capndx+PCI_AGP_STATUS, &agp_bridge->mode);
  250. pci_read_config_byte(agp_bridge->dev, SVWRKS_CACHING, &enable_reg);
  251. enable_reg &= ~0x3;
  252. pci_write_config_byte(agp_bridge->dev, SVWRKS_CACHING, enable_reg);
  253. pci_read_config_byte(agp_bridge->dev, SVWRKS_FEATURE, &enable_reg);
  254. enable_reg |= (1<<6);
  255. pci_write_config_byte(agp_bridge->dev,SVWRKS_FEATURE, enable_reg);
  256. return 0;
  257. }
  258. static void serverworks_cleanup(void)
  259. {
  260. iounmap((void __iomem *) serverworks_private.registers);
  261. }
  262. static int serverworks_insert_memory(struct agp_memory *mem,
  263. off_t pg_start, int type)
  264. {
  265. int i, j, num_entries;
  266. unsigned long __iomem *cur_gatt;
  267. unsigned long addr;
  268. num_entries = A_SIZE_LVL2(agp_bridge->current_size)->num_entries;
  269. if (type != 0 || mem->type != 0) {
  270. return -EINVAL;
  271. }
  272. if ((pg_start + mem->page_count) > num_entries) {
  273. return -EINVAL;
  274. }
  275. j = pg_start;
  276. while (j < (pg_start + mem->page_count)) {
  277. addr = (j * PAGE_SIZE) + agp_bridge->gart_bus_addr;
  278. cur_gatt = SVRWRKS_GET_GATT(addr);
  279. if (!PGE_EMPTY(agp_bridge, readl(cur_gatt+GET_GATT_OFF(addr))))
  280. return -EBUSY;
  281. j++;
  282. }
  283. if (!mem->is_flushed) {
  284. global_cache_flush();
  285. mem->is_flushed = true;
  286. }
  287. for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
  288. addr = (j * PAGE_SIZE) + agp_bridge->gart_bus_addr;
  289. cur_gatt = SVRWRKS_GET_GATT(addr);
  290. writel(agp_bridge->driver->mask_memory(agp_bridge,
  291. page_to_phys(mem->pages[i]), mem->type),
  292. cur_gatt+GET_GATT_OFF(addr));
  293. }
  294. serverworks_tlbflush(mem);
  295. return 0;
  296. }
  297. static int serverworks_remove_memory(struct agp_memory *mem, off_t pg_start,
  298. int type)
  299. {
  300. int i;
  301. unsigned long __iomem *cur_gatt;
  302. unsigned long addr;
  303. if (type != 0 || mem->type != 0) {
  304. return -EINVAL;
  305. }
  306. global_cache_flush();
  307. serverworks_tlbflush(mem);
  308. for (i = pg_start; i < (mem->page_count + pg_start); i++) {
  309. addr = (i * PAGE_SIZE) + agp_bridge->gart_bus_addr;
  310. cur_gatt = SVRWRKS_GET_GATT(addr);
  311. writel(agp_bridge->scratch_page, cur_gatt+GET_GATT_OFF(addr));
  312. }
  313. serverworks_tlbflush(mem);
  314. return 0;
  315. }
  316. static const struct gatt_mask serverworks_masks[] =
  317. {
  318. {.mask = 1, .type = 0}
  319. };
  320. static const struct aper_size_info_lvl2 serverworks_sizes[7] =
  321. {
  322. {2048, 524288, 0x80000000},
  323. {1024, 262144, 0xc0000000},
  324. {512, 131072, 0xe0000000},
  325. {256, 65536, 0xf0000000},
  326. {128, 32768, 0xf8000000},
  327. {64, 16384, 0xfc000000},
  328. {32, 8192, 0xfe000000}
  329. };
  330. static void serverworks_agp_enable(struct agp_bridge_data *bridge, u32 mode)
  331. {
  332. u32 command;
  333. pci_read_config_dword(serverworks_private.svrwrks_dev,
  334. bridge->capndx + PCI_AGP_STATUS,
  335. &command);
  336. command = agp_collect_device_status(bridge, mode, command);
  337. command &= ~0x10; /* disable FW */
  338. command &= ~0x08;
  339. command |= 0x100;
  340. pci_write_config_dword(serverworks_private.svrwrks_dev,
  341. bridge->capndx + PCI_AGP_COMMAND,
  342. command);
  343. agp_device_command(command, false);
  344. }
  345. static const struct agp_bridge_driver sworks_driver = {
  346. .owner = THIS_MODULE,
  347. .aperture_sizes = serverworks_sizes,
  348. .size_type = LVL2_APER_SIZE,
  349. .num_aperture_sizes = 7,
  350. .configure = serverworks_configure,
  351. .fetch_size = serverworks_fetch_size,
  352. .cleanup = serverworks_cleanup,
  353. .tlb_flush = serverworks_tlbflush,
  354. .mask_memory = agp_generic_mask_memory,
  355. .masks = serverworks_masks,
  356. .agp_enable = serverworks_agp_enable,
  357. .cache_flush = global_cache_flush,
  358. .create_gatt_table = serverworks_create_gatt_table,
  359. .free_gatt_table = serverworks_free_gatt_table,
  360. .insert_memory = serverworks_insert_memory,
  361. .remove_memory = serverworks_remove_memory,
  362. .alloc_by_type = agp_generic_alloc_by_type,
  363. .free_by_type = agp_generic_free_by_type,
  364. .agp_alloc_page = agp_generic_alloc_page,
  365. .agp_alloc_pages = agp_generic_alloc_pages,
  366. .agp_destroy_page = agp_generic_destroy_page,
  367. .agp_destroy_pages = agp_generic_destroy_pages,
  368. .agp_type_to_mask_type = agp_generic_type_to_mask_type,
  369. };
  370. static int agp_serverworks_probe(struct pci_dev *pdev,
  371. const struct pci_device_id *ent)
  372. {
  373. struct agp_bridge_data *bridge;
  374. struct pci_dev *bridge_dev;
  375. u32 temp, temp2;
  376. u8 cap_ptr = 0;
  377. cap_ptr = pci_find_capability(pdev, PCI_CAP_ID_AGP);
  378. switch (pdev->device) {
  379. case 0x0006:
  380. dev_err(&pdev->dev, "ServerWorks CNB20HE is unsupported due to lack of documentation\n");
  381. return -ENODEV;
  382. case PCI_DEVICE_ID_SERVERWORKS_HE:
  383. case PCI_DEVICE_ID_SERVERWORKS_LE:
  384. case 0x0007:
  385. break;
  386. default:
  387. if (cap_ptr)
  388. dev_err(&pdev->dev, "unsupported Serverworks chipset "
  389. "[%04x/%04x]\n", pdev->vendor, pdev->device);
  390. return -ENODEV;
  391. }
  392. /* Everything is on func 1 here so we are hardcoding function one */
  393. bridge_dev = pci_get_domain_bus_and_slot(pci_domain_nr(pdev->bus),
  394. (unsigned int)pdev->bus->number,
  395. PCI_DEVFN(0, 1));
  396. if (!bridge_dev) {
  397. dev_info(&pdev->dev, "can't find secondary device\n");
  398. return -ENODEV;
  399. }
  400. serverworks_private.svrwrks_dev = bridge_dev;
  401. serverworks_private.gart_addr_ofs = 0x10;
  402. pci_read_config_dword(pdev, SVWRKS_APSIZE, &temp);
  403. if (temp & PCI_BASE_ADDRESS_MEM_TYPE_64) {
  404. pci_read_config_dword(pdev, SVWRKS_APSIZE + 4, &temp2);
  405. if (temp2 != 0) {
  406. dev_info(&pdev->dev, "64 bit aperture address, "
  407. "but top bits are not zero; disabling AGP\n");
  408. return -ENODEV;
  409. }
  410. serverworks_private.mm_addr_ofs = 0x18;
  411. } else
  412. serverworks_private.mm_addr_ofs = 0x14;
  413. pci_read_config_dword(pdev, serverworks_private.mm_addr_ofs, &temp);
  414. if (temp & PCI_BASE_ADDRESS_MEM_TYPE_64) {
  415. pci_read_config_dword(pdev,
  416. serverworks_private.mm_addr_ofs + 4, &temp2);
  417. if (temp2 != 0) {
  418. dev_info(&pdev->dev, "64 bit MMIO address, but top "
  419. "bits are not zero; disabling AGP\n");
  420. return -ENODEV;
  421. }
  422. }
  423. bridge = agp_alloc_bridge();
  424. if (!bridge)
  425. return -ENOMEM;
  426. bridge->driver = &sworks_driver;
  427. bridge->dev_private_data = &serverworks_private,
  428. bridge->dev = pci_dev_get(pdev);
  429. pci_set_drvdata(pdev, bridge);
  430. return agp_add_bridge(bridge);
  431. }
  432. static void agp_serverworks_remove(struct pci_dev *pdev)
  433. {
  434. struct agp_bridge_data *bridge = pci_get_drvdata(pdev);
  435. pci_dev_put(bridge->dev);
  436. agp_remove_bridge(bridge);
  437. agp_put_bridge(bridge);
  438. pci_dev_put(serverworks_private.svrwrks_dev);
  439. serverworks_private.svrwrks_dev = NULL;
  440. }
  441. static struct pci_device_id agp_serverworks_pci_table[] = {
  442. {
  443. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  444. .class_mask = ~0,
  445. .vendor = PCI_VENDOR_ID_SERVERWORKS,
  446. .device = PCI_ANY_ID,
  447. .subvendor = PCI_ANY_ID,
  448. .subdevice = PCI_ANY_ID,
  449. },
  450. { }
  451. };
  452. MODULE_DEVICE_TABLE(pci, agp_serverworks_pci_table);
  453. static struct pci_driver agp_serverworks_pci_driver = {
  454. .name = "agpgart-serverworks",
  455. .id_table = agp_serverworks_pci_table,
  456. .probe = agp_serverworks_probe,
  457. .remove = agp_serverworks_remove,
  458. };
  459. static int __init agp_serverworks_init(void)
  460. {
  461. if (agp_off)
  462. return -EINVAL;
  463. return pci_register_driver(&agp_serverworks_pci_driver);
  464. }
  465. static void __exit agp_serverworks_cleanup(void)
  466. {
  467. pci_unregister_driver(&agp_serverworks_pci_driver);
  468. }
  469. module_init(agp_serverworks_init);
  470. module_exit(agp_serverworks_cleanup);
  471. MODULE_LICENSE("GPL and additional rights");