tioce_provider.c 28 KB

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