tioce_provider.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2003-2006 Silicon Graphics, Inc. All Rights Reserved.
  7. */
  8. #include <linux/types.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/pci.h>
  11. #include <asm/sn/sn_sal.h>
  12. #include <asm/sn/addrs.h>
  13. #include <asm/sn/io.h>
  14. #include <asm/sn/pcidev.h>
  15. #include <asm/sn/pcibus_provider_defs.h>
  16. #include <asm/sn/tioce_provider.h>
  17. /*
  18. * 1/26/2006
  19. *
  20. * WAR for SGI PV 944642. For revA TIOCE, need to use the following recipe
  21. * (taken from the above PV) before and after accessing tioce internal MMR's
  22. * to avoid tioce lockups.
  23. *
  24. * The recipe as taken from the PV:
  25. *
  26. * if(mmr address < 0x45000) {
  27. * if(mmr address == 0 or 0x80)
  28. * mmr wrt or read address 0xc0
  29. * else if(mmr address == 0x148 or 0x200)
  30. * mmr wrt or read address 0x28
  31. * else
  32. * mmr wrt or read address 0x158
  33. *
  34. * do desired mmr access (rd or wrt)
  35. *
  36. * if(mmr address == 0x100)
  37. * mmr wrt or read address 0x38
  38. * mmr wrt or read address 0xb050
  39. * } else
  40. * do desired mmr access
  41. *
  42. * According to hw, we can use reads instead of writes to the above address
  43. *
  44. * Note this WAR can only to be used for accessing internal MMR's in the
  45. * TIOCE Coretalk Address Range 0x0 - 0x07ff_ffff. This includes the
  46. * "Local CE Registers and Memories" and "PCI Compatible Config Space" address
  47. * spaces from table 2-1 of the "CE Programmer's Reference Overview" document.
  48. *
  49. * All registers defined in struct tioce will meet that criteria.
  50. */
  51. static void inline
  52. tioce_mmr_war_pre(struct tioce_kernel *kern, void __iomem *mmr_addr)
  53. {
  54. u64 mmr_base;
  55. u64 mmr_offset;
  56. if (kern->ce_common->ce_rev != TIOCE_REV_A)
  57. return;
  58. mmr_base = kern->ce_common->ce_pcibus.bs_base;
  59. mmr_offset = (unsigned long)mmr_addr - mmr_base;
  60. if (mmr_offset < 0x45000) {
  61. u64 mmr_war_offset;
  62. if (mmr_offset == 0 || mmr_offset == 0x80)
  63. mmr_war_offset = 0xc0;
  64. else if (mmr_offset == 0x148 || mmr_offset == 0x200)
  65. mmr_war_offset = 0x28;
  66. else
  67. mmr_war_offset = 0x158;
  68. readq_relaxed((void __iomem *)(mmr_base + mmr_war_offset));
  69. }
  70. }
  71. static void inline
  72. tioce_mmr_war_post(struct tioce_kernel *kern, void __iomem *mmr_addr)
  73. {
  74. u64 mmr_base;
  75. u64 mmr_offset;
  76. if (kern->ce_common->ce_rev != TIOCE_REV_A)
  77. return;
  78. mmr_base = kern->ce_common->ce_pcibus.bs_base;
  79. mmr_offset = (unsigned long)mmr_addr - mmr_base;
  80. if (mmr_offset < 0x45000) {
  81. if (mmr_offset == 0x100)
  82. readq_relaxed((void __iomem *)(mmr_base + 0x38));
  83. readq_relaxed((void __iomem *)(mmr_base + 0xb050));
  84. }
  85. }
  86. /* load mmr contents into a variable */
  87. #define tioce_mmr_load(kern, mmrp, varp) do {\
  88. tioce_mmr_war_pre(kern, mmrp); \
  89. *(varp) = readq_relaxed(mmrp); \
  90. tioce_mmr_war_post(kern, mmrp); \
  91. } while (0)
  92. /* store variable contents into mmr */
  93. #define tioce_mmr_store(kern, mmrp, varp) do {\
  94. tioce_mmr_war_pre(kern, mmrp); \
  95. writeq(*varp, mmrp); \
  96. tioce_mmr_war_post(kern, mmrp); \
  97. } while (0)
  98. /* store immediate value into mmr */
  99. #define tioce_mmr_storei(kern, mmrp, val) do {\
  100. tioce_mmr_war_pre(kern, mmrp); \
  101. writeq(val, mmrp); \
  102. tioce_mmr_war_post(kern, mmrp); \
  103. } while (0)
  104. /* set bits (immediate value) into mmr */
  105. #define tioce_mmr_seti(kern, mmrp, bits) do {\
  106. u64 tmp; \
  107. tioce_mmr_load(kern, mmrp, &tmp); \
  108. tmp |= (bits); \
  109. tioce_mmr_store(kern, mmrp, &tmp); \
  110. } while (0)
  111. /* clear bits (immediate value) into mmr */
  112. #define tioce_mmr_clri(kern, mmrp, bits) do { \
  113. u64 tmp; \
  114. tioce_mmr_load(kern, mmrp, &tmp); \
  115. tmp &= ~(bits); \
  116. tioce_mmr_store(kern, mmrp, &tmp); \
  117. } while (0)
  118. /**
  119. * Bus address ranges for the 5 flavors of TIOCE DMA
  120. */
  121. #define TIOCE_D64_MIN 0x8000000000000000UL
  122. #define TIOCE_D64_MAX 0xffffffffffffffffUL
  123. #define TIOCE_D64_ADDR(a) ((a) >= TIOCE_D64_MIN)
  124. #define TIOCE_D32_MIN 0x0000000080000000UL
  125. #define TIOCE_D32_MAX 0x00000000ffffffffUL
  126. #define TIOCE_D32_ADDR(a) ((a) >= TIOCE_D32_MIN && (a) <= TIOCE_D32_MAX)
  127. #define TIOCE_M32_MIN 0x0000000000000000UL
  128. #define TIOCE_M32_MAX 0x000000007fffffffUL
  129. #define TIOCE_M32_ADDR(a) ((a) >= TIOCE_M32_MIN && (a) <= TIOCE_M32_MAX)
  130. #define TIOCE_M40_MIN 0x0000004000000000UL
  131. #define TIOCE_M40_MAX 0x0000007fffffffffUL
  132. #define TIOCE_M40_ADDR(a) ((a) >= TIOCE_M40_MIN && (a) <= TIOCE_M40_MAX)
  133. #define TIOCE_M40S_MIN 0x0000008000000000UL
  134. #define TIOCE_M40S_MAX 0x000000ffffffffffUL
  135. #define TIOCE_M40S_ADDR(a) ((a) >= TIOCE_M40S_MIN && (a) <= TIOCE_M40S_MAX)
  136. /*
  137. * ATE manipulation macros.
  138. */
  139. #define ATE_PAGESHIFT(ps) (__ffs(ps))
  140. #define ATE_PAGEMASK(ps) ((ps)-1)
  141. #define ATE_PAGE(x, ps) ((x) >> ATE_PAGESHIFT(ps))
  142. #define ATE_NPAGES(start, len, pagesize) \
  143. (ATE_PAGE((start)+(len)-1, pagesize) - ATE_PAGE(start, pagesize) + 1)
  144. #define ATE_VALID(ate) ((ate) & (1UL << 63))
  145. #define ATE_MAKE(addr, ps, msi) \
  146. (((addr) & ~ATE_PAGEMASK(ps)) | (1UL << 63) | ((msi)?(1UL << 62):0))
  147. /*
  148. * Flavors of ate-based mapping supported by tioce_alloc_map()
  149. */
  150. #define TIOCE_ATE_M32 1
  151. #define TIOCE_ATE_M40 2
  152. #define TIOCE_ATE_M40S 3
  153. #define KB(x) ((u64)(x) << 10)
  154. #define MB(x) ((u64)(x) << 20)
  155. #define GB(x) ((u64)(x) << 30)
  156. /**
  157. * tioce_dma_d64 - create a DMA mapping using 64-bit direct mode
  158. * @ct_addr: system coretalk address
  159. *
  160. * Map @ct_addr into 64-bit CE bus space. No device context is necessary
  161. * and no CE mapping are consumed.
  162. *
  163. * Bits 53:0 come from the coretalk address. The remaining bits are set as
  164. * follows:
  165. *
  166. * 63 - must be 1 to indicate d64 mode to CE hardware
  167. * 62 - barrier bit ... controlled with tioce_dma_barrier()
  168. * 61 - msi bit ... specified through dma_flags
  169. * 60:54 - reserved, MBZ
  170. */
  171. static u64
  172. tioce_dma_d64(unsigned long ct_addr, int dma_flags)
  173. {
  174. u64 bus_addr;
  175. bus_addr = ct_addr | (1UL << 63);
  176. if (dma_flags & SN_DMA_MSI)
  177. bus_addr |= (1UL << 61);
  178. return bus_addr;
  179. }
  180. /**
  181. * pcidev_to_tioce - return misc ce related pointers given a pci_dev
  182. * @pci_dev: pci device context
  183. * @base: ptr to store struct tioce_mmr * for the CE holding this device
  184. * @kernel: ptr to store struct tioce_kernel * for the CE holding this device
  185. * @port: ptr to store the CE port number that this device is on
  186. *
  187. * Return pointers to various CE-related structures for the CE upstream of
  188. * @pci_dev.
  189. */
  190. static inline void
  191. pcidev_to_tioce(struct pci_dev *pdev, struct tioce __iomem **base,
  192. struct tioce_kernel **kernel, int *port)
  193. {
  194. struct pcidev_info *pcidev_info;
  195. struct tioce_common *ce_common;
  196. struct tioce_kernel *ce_kernel;
  197. pcidev_info = SN_PCIDEV_INFO(pdev);
  198. ce_common = (struct tioce_common *)pcidev_info->pdi_pcibus_info;
  199. ce_kernel = (struct tioce_kernel *)ce_common->ce_kernel_private;
  200. if (base)
  201. *base = (struct tioce __iomem *)ce_common->ce_pcibus.bs_base;
  202. if (kernel)
  203. *kernel = ce_kernel;
  204. /*
  205. * we use port as a zero-based value internally, even though the
  206. * documentation is 1-based.
  207. */
  208. if (port)
  209. *port =
  210. (pdev->bus->number < ce_kernel->ce_port1_secondary) ? 0 : 1;
  211. }
  212. /**
  213. * tioce_alloc_map - Given a coretalk address, map it to pcie bus address
  214. * space using one of the various ATE-based address modes.
  215. * @ce_kern: tioce context
  216. * @type: map mode to use
  217. * @port: 0-based port that the requesting device is downstream of
  218. * @ct_addr: the coretalk address to map
  219. * @len: number of bytes to map
  220. *
  221. * Given the addressing type, set up various parameters that define the
  222. * ATE pool to use. Search for a contiguous block of entries to cover the
  223. * length, and if enough resources exist, fill in the ATEs and construct a
  224. * tioce_dmamap struct to track the mapping.
  225. */
  226. static u64
  227. tioce_alloc_map(struct tioce_kernel *ce_kern, int type, int port,
  228. u64 ct_addr, int len, int dma_flags)
  229. {
  230. int i;
  231. int j;
  232. int first;
  233. int last;
  234. int entries;
  235. int nates;
  236. u64 pagesize;
  237. int msi_capable, msi_wanted;
  238. u64 *ate_shadow;
  239. u64 __iomem *ate_reg;
  240. u64 addr;
  241. struct tioce __iomem *ce_mmr;
  242. u64 bus_base;
  243. struct tioce_dmamap *map;
  244. ce_mmr = (struct tioce __iomem *)ce_kern->ce_common->ce_pcibus.bs_base;
  245. switch (type) {
  246. case TIOCE_ATE_M32:
  247. /*
  248. * The first 64 entries of the ate3240 pool are dedicated to
  249. * super-page (TIOCE_ATE_M40S) mode.
  250. */
  251. first = 64;
  252. entries = TIOCE_NUM_M3240_ATES - 64;
  253. ate_shadow = ce_kern->ce_ate3240_shadow;
  254. ate_reg = ce_mmr->ce_ure_ate3240;
  255. pagesize = ce_kern->ce_ate3240_pagesize;
  256. bus_base = TIOCE_M32_MIN;
  257. msi_capable = 1;
  258. break;
  259. case TIOCE_ATE_M40:
  260. first = 0;
  261. entries = TIOCE_NUM_M40_ATES;
  262. ate_shadow = ce_kern->ce_ate40_shadow;
  263. ate_reg = ce_mmr->ce_ure_ate40;
  264. pagesize = MB(64);
  265. bus_base = TIOCE_M40_MIN;
  266. msi_capable = 0;
  267. break;
  268. case TIOCE_ATE_M40S:
  269. /*
  270. * ate3240 entries 0-31 are dedicated to port1 super-page
  271. * mappings. ate3240 entries 32-63 are dedicated to port2.
  272. */
  273. first = port * 32;
  274. entries = 32;
  275. ate_shadow = ce_kern->ce_ate3240_shadow;
  276. ate_reg = ce_mmr->ce_ure_ate3240;
  277. pagesize = GB(16);
  278. bus_base = TIOCE_M40S_MIN;
  279. msi_capable = 0;
  280. break;
  281. default:
  282. return 0;
  283. }
  284. msi_wanted = dma_flags & SN_DMA_MSI;
  285. if (msi_wanted && !msi_capable)
  286. return 0;
  287. nates = ATE_NPAGES(ct_addr, len, pagesize);
  288. if (nates > entries)
  289. return 0;
  290. last = first + entries - nates;
  291. for (i = first; i <= last; i++) {
  292. if (ATE_VALID(ate_shadow[i]))
  293. continue;
  294. for (j = i; j < i + nates; j++)
  295. if (ATE_VALID(ate_shadow[j]))
  296. break;
  297. if (j >= i + nates)
  298. break;
  299. }
  300. if (i > last)
  301. return 0;
  302. map = kzalloc(sizeof(struct tioce_dmamap), GFP_ATOMIC);
  303. if (!map)
  304. return 0;
  305. addr = ct_addr;
  306. for (j = 0; j < nates; j++) {
  307. u64 ate;
  308. ate = ATE_MAKE(addr, pagesize, msi_wanted);
  309. ate_shadow[i + j] = ate;
  310. tioce_mmr_storei(ce_kern, &ate_reg[i + j], ate);
  311. addr += pagesize;
  312. }
  313. map->refcnt = 1;
  314. map->nbytes = nates * pagesize;
  315. map->ct_start = ct_addr & ~ATE_PAGEMASK(pagesize);
  316. map->pci_start = bus_base + (i * pagesize);
  317. map->ate_hw = &ate_reg[i];
  318. map->ate_shadow = &ate_shadow[i];
  319. map->ate_count = nates;
  320. list_add(&map->ce_dmamap_list, &ce_kern->ce_dmamap_list);
  321. return (map->pci_start + (ct_addr - map->ct_start));
  322. }
  323. /**
  324. * tioce_dma_d32 - create a DMA mapping using 32-bit direct mode
  325. * @pdev: linux pci_dev representing the function
  326. * @paddr: system physical address
  327. *
  328. * Map @paddr into 32-bit bus space of the CE associated with @pcidev_info.
  329. */
  330. static u64
  331. tioce_dma_d32(struct pci_dev *pdev, u64 ct_addr, int dma_flags)
  332. {
  333. int dma_ok;
  334. int port;
  335. struct tioce __iomem *ce_mmr;
  336. struct tioce_kernel *ce_kern;
  337. u64 ct_upper;
  338. u64 ct_lower;
  339. dma_addr_t bus_addr;
  340. if (dma_flags & SN_DMA_MSI)
  341. return 0;
  342. ct_upper = ct_addr & ~0x3fffffffUL;
  343. ct_lower = ct_addr & 0x3fffffffUL;
  344. pcidev_to_tioce(pdev, &ce_mmr, &ce_kern, &port);
  345. if (ce_kern->ce_port[port].dirmap_refcnt == 0) {
  346. u64 tmp;
  347. ce_kern->ce_port[port].dirmap_shadow = ct_upper;
  348. tioce_mmr_storei(ce_kern, &ce_mmr->ce_ure_dir_map[port],
  349. ct_upper);
  350. tmp = ce_mmr->ce_ure_dir_map[port];
  351. dma_ok = 1;
  352. } else
  353. dma_ok = (ce_kern->ce_port[port].dirmap_shadow == ct_upper);
  354. if (dma_ok) {
  355. ce_kern->ce_port[port].dirmap_refcnt++;
  356. bus_addr = TIOCE_D32_MIN + ct_lower;
  357. } else
  358. bus_addr = 0;
  359. return bus_addr;
  360. }
  361. /**
  362. * tioce_dma_barrier - swizzle a TIOCE bus address to include or exclude
  363. * the barrier bit.
  364. * @bus_addr: bus address to swizzle
  365. *
  366. * Given a TIOCE bus address, set the appropriate bit to indicate barrier
  367. * attributes.
  368. */
  369. static u64
  370. tioce_dma_barrier(u64 bus_addr, int on)
  371. {
  372. u64 barrier_bit;
  373. /* barrier not supported in M40/M40S mode */
  374. if (TIOCE_M40_ADDR(bus_addr) || TIOCE_M40S_ADDR(bus_addr))
  375. return bus_addr;
  376. if (TIOCE_D64_ADDR(bus_addr))
  377. barrier_bit = (1UL << 62);
  378. else /* must be m32 or d32 */
  379. barrier_bit = (1UL << 30);
  380. return (on) ? (bus_addr | barrier_bit) : (bus_addr & ~barrier_bit);
  381. }
  382. /**
  383. * tioce_dma_unmap - release CE mapping resources
  384. * @pdev: linux pci_dev representing the function
  385. * @bus_addr: bus address returned by an earlier tioce_dma_map
  386. * @dir: mapping direction (unused)
  387. *
  388. * Locate mapping resources associated with @bus_addr and release them.
  389. * For mappings created using the direct modes there are no resources
  390. * to release.
  391. */
  392. void
  393. tioce_dma_unmap(struct pci_dev *pdev, dma_addr_t bus_addr, int dir)
  394. {
  395. int i;
  396. int port;
  397. struct tioce_kernel *ce_kern;
  398. struct tioce __iomem *ce_mmr;
  399. unsigned long flags;
  400. bus_addr = tioce_dma_barrier(bus_addr, 0);
  401. pcidev_to_tioce(pdev, &ce_mmr, &ce_kern, &port);
  402. /* nothing to do for D64 */
  403. if (TIOCE_D64_ADDR(bus_addr))
  404. return;
  405. spin_lock_irqsave(&ce_kern->ce_lock, flags);
  406. if (TIOCE_D32_ADDR(bus_addr)) {
  407. if (--ce_kern->ce_port[port].dirmap_refcnt == 0) {
  408. ce_kern->ce_port[port].dirmap_shadow = 0;
  409. tioce_mmr_storei(ce_kern, &ce_mmr->ce_ure_dir_map[port],
  410. 0);
  411. }
  412. } else {
  413. struct tioce_dmamap *map;
  414. list_for_each_entry(map, &ce_kern->ce_dmamap_list,
  415. ce_dmamap_list) {
  416. u64 last;
  417. last = map->pci_start + map->nbytes - 1;
  418. if (bus_addr >= map->pci_start && bus_addr <= last)
  419. break;
  420. }
  421. if (&map->ce_dmamap_list == &ce_kern->ce_dmamap_list) {
  422. printk(KERN_WARNING
  423. "%s: %s - no map found for bus_addr 0x%llx\n",
  424. __func__, pci_name(pdev), bus_addr);
  425. } else if (--map->refcnt == 0) {
  426. for (i = 0; i < map->ate_count; i++) {
  427. map->ate_shadow[i] = 0;
  428. tioce_mmr_storei(ce_kern, &map->ate_hw[i], 0);
  429. }
  430. list_del(&map->ce_dmamap_list);
  431. kfree(map);
  432. }
  433. }
  434. spin_unlock_irqrestore(&ce_kern->ce_lock, flags);
  435. }
  436. /**
  437. * tioce_do_dma_map - map pages for PCI DMA
  438. * @pdev: linux pci_dev representing the function
  439. * @paddr: host physical address to map
  440. * @byte_count: bytes to map
  441. *
  442. * This is the main wrapper for mapping host physical pages to CE PCI space.
  443. * The mapping mode used is based on the device's dma_mask.
  444. */
  445. static u64
  446. tioce_do_dma_map(struct pci_dev *pdev, u64 paddr, size_t byte_count,
  447. int barrier, int dma_flags)
  448. {
  449. unsigned long flags;
  450. u64 ct_addr;
  451. u64 mapaddr = 0;
  452. struct tioce_kernel *ce_kern;
  453. struct tioce_dmamap *map;
  454. int port;
  455. u64 dma_mask;
  456. dma_mask = (barrier) ? pdev->dev.coherent_dma_mask : pdev->dma_mask;
  457. /* cards must be able to address at least 31 bits */
  458. if (dma_mask < 0x7fffffffUL)
  459. return 0;
  460. if (SN_DMA_ADDRTYPE(dma_flags) == SN_DMA_ADDR_PHYS)
  461. ct_addr = PHYS_TO_TIODMA(paddr);
  462. else
  463. ct_addr = paddr;
  464. /*
  465. * If the device can generate 64 bit addresses, create a D64 map.
  466. */
  467. if (dma_mask == ~0UL) {
  468. mapaddr = tioce_dma_d64(ct_addr, dma_flags);
  469. if (mapaddr)
  470. goto dma_map_done;
  471. }
  472. pcidev_to_tioce(pdev, NULL, &ce_kern, &port);
  473. spin_lock_irqsave(&ce_kern->ce_lock, flags);
  474. /*
  475. * D64 didn't work ... See if we have an existing map that covers
  476. * this address range. Must account for devices dma_mask here since
  477. * an existing map might have been done in a mode using more pci
  478. * address bits than this device can support.
  479. */
  480. list_for_each_entry(map, &ce_kern->ce_dmamap_list, ce_dmamap_list) {
  481. u64 last;
  482. last = map->ct_start + map->nbytes - 1;
  483. if (ct_addr >= map->ct_start &&
  484. ct_addr + byte_count - 1 <= last &&
  485. map->pci_start <= dma_mask) {
  486. map->refcnt++;
  487. mapaddr = map->pci_start + (ct_addr - map->ct_start);
  488. break;
  489. }
  490. }
  491. /*
  492. * If we don't have a map yet, and the card can generate 40
  493. * bit addresses, try the M40/M40S modes. Note these modes do not
  494. * support a barrier bit, so if we need a consistent map these
  495. * won't work.
  496. */
  497. if (!mapaddr && !barrier && dma_mask >= 0xffffffffffUL) {
  498. /*
  499. * We have two options for 40-bit mappings: 16GB "super" ATEs
  500. * and 64MB "regular" ATEs. We'll try both if needed for a
  501. * given mapping but which one we try first depends on the
  502. * size. For requests >64MB, prefer to use a super page with
  503. * regular as the fallback. Otherwise, try in the reverse order.
  504. */
  505. if (byte_count > MB(64)) {
  506. mapaddr = tioce_alloc_map(ce_kern, TIOCE_ATE_M40S,
  507. port, ct_addr, byte_count,
  508. dma_flags);
  509. if (!mapaddr)
  510. mapaddr =
  511. tioce_alloc_map(ce_kern, TIOCE_ATE_M40, -1,
  512. ct_addr, byte_count,
  513. dma_flags);
  514. } else {
  515. mapaddr = tioce_alloc_map(ce_kern, TIOCE_ATE_M40, -1,
  516. ct_addr, byte_count,
  517. dma_flags);
  518. if (!mapaddr)
  519. mapaddr =
  520. tioce_alloc_map(ce_kern, TIOCE_ATE_M40S,
  521. port, ct_addr, byte_count,
  522. dma_flags);
  523. }
  524. }
  525. /*
  526. * 32-bit direct is the next mode to try
  527. */
  528. if (!mapaddr && dma_mask >= 0xffffffffUL)
  529. mapaddr = tioce_dma_d32(pdev, ct_addr, dma_flags);
  530. /*
  531. * Last resort, try 32-bit ATE-based map.
  532. */
  533. if (!mapaddr)
  534. mapaddr =
  535. tioce_alloc_map(ce_kern, TIOCE_ATE_M32, -1, ct_addr,
  536. byte_count, dma_flags);
  537. spin_unlock_irqrestore(&ce_kern->ce_lock, flags);
  538. dma_map_done:
  539. if (mapaddr && barrier)
  540. mapaddr = tioce_dma_barrier(mapaddr, 1);
  541. return mapaddr;
  542. }
  543. /**
  544. * tioce_dma - standard pci dma map interface
  545. * @pdev: pci device requesting the map
  546. * @paddr: system physical address to map into pci space
  547. * @byte_count: # bytes to map
  548. *
  549. * Simply call tioce_do_dma_map() to create a map with the barrier bit clear
  550. * in the address.
  551. */
  552. static u64
  553. tioce_dma(struct pci_dev *pdev, unsigned long paddr, size_t byte_count, int dma_flags)
  554. {
  555. return tioce_do_dma_map(pdev, paddr, byte_count, 0, dma_flags);
  556. }
  557. /**
  558. * tioce_dma_consistent - consistent pci dma map interface
  559. * @pdev: pci device requesting the map
  560. * @paddr: system physical address to map into pci space
  561. * @byte_count: # bytes to map
  562. *
  563. * Simply call tioce_do_dma_map() to create a map with the barrier bit set
  564. * in the address.
  565. */
  566. static u64
  567. tioce_dma_consistent(struct pci_dev *pdev, unsigned long paddr, size_t byte_count, int dma_flags)
  568. {
  569. return tioce_do_dma_map(pdev, paddr, byte_count, 1, dma_flags);
  570. }
  571. /**
  572. * tioce_error_intr_handler - SGI TIO CE error interrupt handler
  573. * @irq: unused
  574. * @arg: pointer to tioce_common struct for the given CE
  575. *
  576. * Handle a CE error interrupt. Simply a wrapper around a SAL call which
  577. * defers processing to the SGI prom.
  578. */
  579. static irqreturn_t
  580. tioce_error_intr_handler(int irq, void *arg)
  581. {
  582. struct tioce_common *soft = arg;
  583. struct ia64_sal_retval ret_stuff;
  584. ret_stuff.status = 0;
  585. ret_stuff.v0 = 0;
  586. SAL_CALL_NOLOCK(ret_stuff, (u64) SN_SAL_IOIF_ERROR_INTERRUPT,
  587. soft->ce_pcibus.bs_persist_segment,
  588. soft->ce_pcibus.bs_persist_busnum, 0, 0, 0, 0, 0);
  589. if (ret_stuff.v0)
  590. panic("tioce_error_intr_handler: Fatal TIOCE error");
  591. return IRQ_HANDLED;
  592. }
  593. /**
  594. * tioce_reserve_m32 - reserve M32 ATEs for the indicated address range
  595. * @tioce_kernel: TIOCE context to reserve ATEs for
  596. * @base: starting bus address to reserve
  597. * @limit: last bus address to reserve
  598. *
  599. * If base/limit falls within the range of bus space mapped through the
  600. * M32 space, reserve the resources corresponding to the range.
  601. */
  602. static void
  603. tioce_reserve_m32(struct tioce_kernel *ce_kern, u64 base, u64 limit)
  604. {
  605. int ate_index, last_ate, ps;
  606. struct tioce __iomem *ce_mmr;
  607. ce_mmr = (struct tioce __iomem *)ce_kern->ce_common->ce_pcibus.bs_base;
  608. ps = ce_kern->ce_ate3240_pagesize;
  609. ate_index = ATE_PAGE(base, ps);
  610. last_ate = ate_index + ATE_NPAGES(base, limit-base+1, ps) - 1;
  611. if (ate_index < 64)
  612. ate_index = 64;
  613. if (last_ate >= TIOCE_NUM_M3240_ATES)
  614. last_ate = TIOCE_NUM_M3240_ATES - 1;
  615. while (ate_index <= last_ate) {
  616. u64 ate;
  617. ate = ATE_MAKE(0xdeadbeef, ps, 0);
  618. ce_kern->ce_ate3240_shadow[ate_index] = ate;
  619. tioce_mmr_storei(ce_kern, &ce_mmr->ce_ure_ate3240[ate_index],
  620. ate);
  621. ate_index++;
  622. }
  623. }
  624. /**
  625. * tioce_kern_init - init kernel structures related to a given TIOCE
  626. * @tioce_common: ptr to a cached tioce_common struct that originated in prom
  627. */
  628. static struct tioce_kernel *
  629. tioce_kern_init(struct tioce_common *tioce_common)
  630. {
  631. int i;
  632. int ps;
  633. int dev;
  634. u32 tmp;
  635. unsigned int seg, bus;
  636. struct tioce __iomem *tioce_mmr;
  637. struct tioce_kernel *tioce_kern;
  638. tioce_kern = kzalloc(sizeof(struct tioce_kernel), GFP_KERNEL);
  639. if (!tioce_kern) {
  640. return NULL;
  641. }
  642. tioce_kern->ce_common = tioce_common;
  643. spin_lock_init(&tioce_kern->ce_lock);
  644. INIT_LIST_HEAD(&tioce_kern->ce_dmamap_list);
  645. tioce_common->ce_kernel_private = (u64) tioce_kern;
  646. /*
  647. * Determine the secondary bus number of the port2 logical PPB.
  648. * This is used to decide whether a given pci device resides on
  649. * port1 or port2. Note: We don't have enough plumbing set up
  650. * here to use pci_read_config_xxx() so use raw_pci_read().
  651. */
  652. seg = tioce_common->ce_pcibus.bs_persist_segment;
  653. bus = tioce_common->ce_pcibus.bs_persist_busnum;
  654. raw_pci_read(seg, bus, PCI_DEVFN(2, 0), PCI_SECONDARY_BUS, 1,&tmp);
  655. tioce_kern->ce_port1_secondary = (u8) tmp;
  656. /*
  657. * Set PMU pagesize to the largest size available, and zero out
  658. * the ATEs.
  659. */
  660. tioce_mmr = (struct tioce __iomem *)tioce_common->ce_pcibus.bs_base;
  661. tioce_mmr_clri(tioce_kern, &tioce_mmr->ce_ure_page_map,
  662. CE_URE_PAGESIZE_MASK);
  663. tioce_mmr_seti(tioce_kern, &tioce_mmr->ce_ure_page_map,
  664. CE_URE_256K_PAGESIZE);
  665. ps = tioce_kern->ce_ate3240_pagesize = KB(256);
  666. for (i = 0; i < TIOCE_NUM_M40_ATES; i++) {
  667. tioce_kern->ce_ate40_shadow[i] = 0;
  668. tioce_mmr_storei(tioce_kern, &tioce_mmr->ce_ure_ate40[i], 0);
  669. }
  670. for (i = 0; i < TIOCE_NUM_M3240_ATES; i++) {
  671. tioce_kern->ce_ate3240_shadow[i] = 0;
  672. tioce_mmr_storei(tioce_kern, &tioce_mmr->ce_ure_ate3240[i], 0);
  673. }
  674. /*
  675. * Reserve ATEs corresponding to reserved address ranges. These
  676. * include:
  677. *
  678. * Memory space covered by each PPB mem base/limit register
  679. * Memory space covered by each PPB prefetch base/limit register
  680. *
  681. * These bus ranges are for pio (downstream) traffic only, and so
  682. * cannot be used for DMA.
  683. */
  684. for (dev = 1; dev <= 2; dev++) {
  685. u64 base, limit;
  686. /* mem base/limit */
  687. raw_pci_read(seg, bus, PCI_DEVFN(dev, 0),
  688. PCI_MEMORY_BASE, 2, &tmp);
  689. base = (u64)tmp << 16;
  690. raw_pci_read(seg, bus, PCI_DEVFN(dev, 0),
  691. PCI_MEMORY_LIMIT, 2, &tmp);
  692. limit = (u64)tmp << 16;
  693. limit |= 0xfffffUL;
  694. if (base < limit)
  695. tioce_reserve_m32(tioce_kern, base, limit);
  696. /*
  697. * prefetch mem base/limit. The tioce ppb's have 64-bit
  698. * decoders, so read the upper portions w/o checking the
  699. * attributes.
  700. */
  701. raw_pci_read(seg, bus, PCI_DEVFN(dev, 0),
  702. PCI_PREF_MEMORY_BASE, 2, &tmp);
  703. base = ((u64)tmp & PCI_PREF_RANGE_MASK) << 16;
  704. raw_pci_read(seg, bus, PCI_DEVFN(dev, 0),
  705. PCI_PREF_BASE_UPPER32, 4, &tmp);
  706. base |= (u64)tmp << 32;
  707. raw_pci_read(seg, bus, PCI_DEVFN(dev, 0),
  708. PCI_PREF_MEMORY_LIMIT, 2, &tmp);
  709. limit = ((u64)tmp & PCI_PREF_RANGE_MASK) << 16;
  710. limit |= 0xfffffUL;
  711. raw_pci_read(seg, bus, PCI_DEVFN(dev, 0),
  712. PCI_PREF_LIMIT_UPPER32, 4, &tmp);
  713. limit |= (u64)tmp << 32;
  714. if ((base < limit) && TIOCE_M32_ADDR(base))
  715. tioce_reserve_m32(tioce_kern, base, limit);
  716. }
  717. return tioce_kern;
  718. }
  719. /**
  720. * tioce_force_interrupt - implement altix force_interrupt() backend for CE
  721. * @sn_irq_info: sn asic irq that we need an interrupt generated for
  722. *
  723. * Given an sn_irq_info struct, set the proper bit in ce_adm_force_int to
  724. * force a secondary interrupt to be generated. This is to work around an
  725. * asic issue where there is a small window of opportunity for a legacy device
  726. * interrupt to be lost.
  727. */
  728. static void
  729. tioce_force_interrupt(struct sn_irq_info *sn_irq_info)
  730. {
  731. struct pcidev_info *pcidev_info;
  732. struct tioce_common *ce_common;
  733. struct tioce_kernel *ce_kern;
  734. struct tioce __iomem *ce_mmr;
  735. u64 force_int_val;
  736. if (!sn_irq_info->irq_bridge)
  737. return;
  738. if (sn_irq_info->irq_bridge_type != PCIIO_ASIC_TYPE_TIOCE)
  739. return;
  740. pcidev_info = (struct pcidev_info *)sn_irq_info->irq_pciioinfo;
  741. if (!pcidev_info)
  742. return;
  743. ce_common = (struct tioce_common *)pcidev_info->pdi_pcibus_info;
  744. ce_mmr = (struct tioce __iomem *)ce_common->ce_pcibus.bs_base;
  745. ce_kern = (struct tioce_kernel *)ce_common->ce_kernel_private;
  746. /*
  747. * TIOCE Rev A workaround (PV 945826), force an interrupt by writing
  748. * the TIO_INTx register directly (1/26/2006)
  749. */
  750. if (ce_common->ce_rev == TIOCE_REV_A) {
  751. u64 int_bit_mask = (1ULL << sn_irq_info->irq_int_bit);
  752. u64 status;
  753. tioce_mmr_load(ce_kern, &ce_mmr->ce_adm_int_status, &status);
  754. if (status & int_bit_mask) {
  755. u64 force_irq = (1 << 8) | sn_irq_info->irq_irq;
  756. u64 ctalk = sn_irq_info->irq_xtalkaddr;
  757. u64 nasid, offset;
  758. nasid = (ctalk & CTALK_NASID_MASK) >> CTALK_NASID_SHFT;
  759. offset = (ctalk & CTALK_NODE_OFFSET);
  760. HUB_S(TIO_IOSPACE_ADDR(nasid, offset), force_irq);
  761. }
  762. return;
  763. }
  764. /*
  765. * irq_int_bit is originally set up by prom, and holds the interrupt
  766. * bit shift (not mask) as defined by the bit definitions in the
  767. * ce_adm_int mmr. These shifts are not the same for the
  768. * ce_adm_force_int register, so do an explicit mapping here to make
  769. * things clearer.
  770. */
  771. switch (sn_irq_info->irq_int_bit) {
  772. case CE_ADM_INT_PCIE_PORT1_DEV_A_SHFT:
  773. force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT1_DEV_A_SHFT;
  774. break;
  775. case CE_ADM_INT_PCIE_PORT1_DEV_B_SHFT:
  776. force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT1_DEV_B_SHFT;
  777. break;
  778. case CE_ADM_INT_PCIE_PORT1_DEV_C_SHFT:
  779. force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT1_DEV_C_SHFT;
  780. break;
  781. case CE_ADM_INT_PCIE_PORT1_DEV_D_SHFT:
  782. force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT1_DEV_D_SHFT;
  783. break;
  784. case CE_ADM_INT_PCIE_PORT2_DEV_A_SHFT:
  785. force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT2_DEV_A_SHFT;
  786. break;
  787. case CE_ADM_INT_PCIE_PORT2_DEV_B_SHFT:
  788. force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT2_DEV_B_SHFT;
  789. break;
  790. case CE_ADM_INT_PCIE_PORT2_DEV_C_SHFT:
  791. force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT2_DEV_C_SHFT;
  792. break;
  793. case CE_ADM_INT_PCIE_PORT2_DEV_D_SHFT:
  794. force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT2_DEV_D_SHFT;
  795. break;
  796. default:
  797. return;
  798. }
  799. tioce_mmr_storei(ce_kern, &ce_mmr->ce_adm_force_int, force_int_val);
  800. }
  801. /**
  802. * tioce_target_interrupt - implement set_irq_affinity for tioce resident
  803. * functions. Note: only applies to line interrupts, not MSI's.
  804. *
  805. * @sn_irq_info: SN IRQ context
  806. *
  807. * Given an sn_irq_info, set the associated CE device's interrupt destination
  808. * register. Since the interrupt destination registers are on a per-ce-slot
  809. * basis, this will retarget line interrupts for all functions downstream of
  810. * the slot.
  811. */
  812. static void
  813. tioce_target_interrupt(struct sn_irq_info *sn_irq_info)
  814. {
  815. struct pcidev_info *pcidev_info;
  816. struct tioce_common *ce_common;
  817. struct tioce_kernel *ce_kern;
  818. struct tioce __iomem *ce_mmr;
  819. int bit;
  820. u64 vector;
  821. pcidev_info = (struct pcidev_info *)sn_irq_info->irq_pciioinfo;
  822. if (!pcidev_info)
  823. return;
  824. ce_common = (struct tioce_common *)pcidev_info->pdi_pcibus_info;
  825. ce_mmr = (struct tioce __iomem *)ce_common->ce_pcibus.bs_base;
  826. ce_kern = (struct tioce_kernel *)ce_common->ce_kernel_private;
  827. bit = sn_irq_info->irq_int_bit;
  828. tioce_mmr_seti(ce_kern, &ce_mmr->ce_adm_int_mask, (1UL << bit));
  829. vector = (u64)sn_irq_info->irq_irq << INTR_VECTOR_SHFT;
  830. vector |= sn_irq_info->irq_xtalkaddr;
  831. tioce_mmr_storei(ce_kern, &ce_mmr->ce_adm_int_dest[bit], vector);
  832. tioce_mmr_clri(ce_kern, &ce_mmr->ce_adm_int_mask, (1UL << bit));
  833. tioce_force_interrupt(sn_irq_info);
  834. }
  835. /**
  836. * tioce_bus_fixup - perform final PCI fixup for a TIO CE bus
  837. * @prom_bussoft: Common prom/kernel struct representing the bus
  838. *
  839. * Replicates the tioce_common pointed to by @prom_bussoft in kernel
  840. * space. Allocates and initializes a kernel-only area for a given CE,
  841. * and sets up an irq for handling CE error interrupts.
  842. *
  843. * On successful setup, returns the kernel version of tioce_common back to
  844. * the caller.
  845. */
  846. static void *
  847. tioce_bus_fixup(struct pcibus_bussoft *prom_bussoft, struct pci_controller *controller)
  848. {
  849. struct tioce_common *tioce_common;
  850. struct tioce_kernel *tioce_kern;
  851. struct tioce __iomem *tioce_mmr;
  852. /*
  853. * Allocate kernel bus soft and copy from prom.
  854. */
  855. tioce_common = kzalloc(sizeof(struct tioce_common), GFP_KERNEL);
  856. if (!tioce_common)
  857. return NULL;
  858. memcpy(tioce_common, prom_bussoft, sizeof(struct tioce_common));
  859. tioce_common->ce_pcibus.bs_base = (unsigned long)
  860. ioremap(REGION_OFFSET(tioce_common->ce_pcibus.bs_base),
  861. sizeof(struct tioce_common));
  862. tioce_kern = tioce_kern_init(tioce_common);
  863. if (tioce_kern == NULL) {
  864. kfree(tioce_common);
  865. return NULL;
  866. }
  867. /*
  868. * Clear out any transient errors before registering the error
  869. * interrupt handler.
  870. */
  871. tioce_mmr = (struct tioce __iomem *)tioce_common->ce_pcibus.bs_base;
  872. tioce_mmr_seti(tioce_kern, &tioce_mmr->ce_adm_int_status_alias, ~0ULL);
  873. tioce_mmr_seti(tioce_kern, &tioce_mmr->ce_adm_error_summary_alias,
  874. ~0ULL);
  875. tioce_mmr_seti(tioce_kern, &tioce_mmr->ce_dre_comp_err_addr, 0ULL);
  876. if (request_irq(SGI_PCIASIC_ERROR,
  877. tioce_error_intr_handler,
  878. IRQF_SHARED, "TIOCE error", (void *)tioce_common))
  879. printk(KERN_WARNING
  880. "%s: Unable to get irq %d. "
  881. "Error interrupts won't be routed for "
  882. "TIOCE bus %04x:%02x\n",
  883. __func__, SGI_PCIASIC_ERROR,
  884. tioce_common->ce_pcibus.bs_persist_segment,
  885. tioce_common->ce_pcibus.bs_persist_busnum);
  886. sn_set_err_irq_affinity(SGI_PCIASIC_ERROR);
  887. return tioce_common;
  888. }
  889. static struct sn_pcibus_provider tioce_pci_interfaces = {
  890. .dma_map = tioce_dma,
  891. .dma_map_consistent = tioce_dma_consistent,
  892. .dma_unmap = tioce_dma_unmap,
  893. .bus_fixup = tioce_bus_fixup,
  894. .force_interrupt = tioce_force_interrupt,
  895. .target_interrupt = tioce_target_interrupt
  896. };
  897. /**
  898. * tioce_init_provider - init SN PCI provider ops for TIO CE
  899. */
  900. int
  901. tioce_init_provider(void)
  902. {
  903. sn_pci_provider[PCIIO_ASIC_TYPE_TIOCE] = &tioce_pci_interfaces;
  904. return 0;
  905. }