iSeries_pci.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. /*
  2. * iSeries_pci.c
  3. *
  4. * Copyright (C) 2001 Allan Trautman, IBM Corporation
  5. *
  6. * iSeries specific routines for PCI.
  7. *
  8. * Based on code from pci.c and iSeries_pci.c 32bit
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/list.h>
  26. #include <linux/string.h>
  27. #include <linux/init.h>
  28. #include <linux/module.h>
  29. #include <linux/ide.h>
  30. #include <linux/pci.h>
  31. #include <asm/io.h>
  32. #include <asm/irq.h>
  33. #include <asm/prom.h>
  34. #include <asm/machdep.h>
  35. #include <asm/pci-bridge.h>
  36. #include <asm/ppcdebug.h>
  37. #include <asm/iommu.h>
  38. #include <asm/iSeries/HvCallPci.h>
  39. #include <asm/iSeries/HvCallSm.h>
  40. #include <asm/iSeries/HvCallXm.h>
  41. #include <asm/iSeries/iSeries_irq.h>
  42. #include <asm/iSeries/iSeries_pci.h>
  43. #include <asm/iSeries/mf.h>
  44. #include "pci.h"
  45. extern unsigned long io_page_mask;
  46. /*
  47. * Forward declares of prototypes.
  48. */
  49. static struct iSeries_Device_Node *find_Device_Node(int bus, int devfn);
  50. static void scan_PHB_slots(struct pci_controller *Phb);
  51. static void scan_EADS_bridge(HvBusNumber Bus, HvSubBusNumber SubBus, int IdSel);
  52. static int scan_bridge_slot(HvBusNumber Bus, struct HvCallPci_BridgeInfo *Info);
  53. LIST_HEAD(iSeries_Global_Device_List);
  54. static int DeviceCount;
  55. /* Counters and control flags. */
  56. static long Pci_Io_Read_Count;
  57. static long Pci_Io_Write_Count;
  58. #if 0
  59. static long Pci_Cfg_Read_Count;
  60. static long Pci_Cfg_Write_Count;
  61. #endif
  62. static long Pci_Error_Count;
  63. static int Pci_Retry_Max = 3; /* Only retry 3 times */
  64. static int Pci_Error_Flag = 1; /* Set Retry Error on. */
  65. static struct pci_ops iSeries_pci_ops;
  66. /*
  67. * Table defines
  68. * Each Entry size is 4 MB * 1024 Entries = 4GB I/O address space.
  69. */
  70. #define IOMM_TABLE_MAX_ENTRIES 1024
  71. #define IOMM_TABLE_ENTRY_SIZE 0x0000000000400000UL
  72. #define BASE_IO_MEMORY 0xE000000000000000UL
  73. static unsigned long max_io_memory = 0xE000000000000000UL;
  74. static long current_iomm_table_entry;
  75. /*
  76. * Lookup Tables.
  77. */
  78. static struct iSeries_Device_Node **iomm_table;
  79. static u8 *iobar_table;
  80. /*
  81. * Static and Global variables
  82. */
  83. static char *pci_io_text = "iSeries PCI I/O";
  84. static DEFINE_SPINLOCK(iomm_table_lock);
  85. /*
  86. * iomm_table_initialize
  87. *
  88. * Allocates and initalizes the Address Translation Table and Bar
  89. * Tables to get them ready for use. Must be called before any
  90. * I/O space is handed out to the device BARs.
  91. */
  92. static void iomm_table_initialize(void)
  93. {
  94. spin_lock(&iomm_table_lock);
  95. iomm_table = kmalloc(sizeof(*iomm_table) * IOMM_TABLE_MAX_ENTRIES,
  96. GFP_KERNEL);
  97. iobar_table = kmalloc(sizeof(*iobar_table) * IOMM_TABLE_MAX_ENTRIES,
  98. GFP_KERNEL);
  99. spin_unlock(&iomm_table_lock);
  100. if ((iomm_table == NULL) || (iobar_table == NULL))
  101. panic("PCI: I/O tables allocation failed.\n");
  102. }
  103. /*
  104. * iomm_table_allocate_entry
  105. *
  106. * Adds pci_dev entry in address translation table
  107. *
  108. * - Allocates the number of entries required in table base on BAR
  109. * size.
  110. * - Allocates starting at BASE_IO_MEMORY and increases.
  111. * - The size is round up to be a multiple of entry size.
  112. * - CurrentIndex is incremented to keep track of the last entry.
  113. * - Builds the resource entry for allocated BARs.
  114. */
  115. static void iomm_table_allocate_entry(struct pci_dev *dev, int bar_num)
  116. {
  117. struct resource *bar_res = &dev->resource[bar_num];
  118. long bar_size = pci_resource_len(dev, bar_num);
  119. /*
  120. * No space to allocate, quick exit, skip Allocation.
  121. */
  122. if (bar_size == 0)
  123. return;
  124. /*
  125. * Set Resource values.
  126. */
  127. spin_lock(&iomm_table_lock);
  128. bar_res->name = pci_io_text;
  129. bar_res->start =
  130. IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry;
  131. bar_res->start += BASE_IO_MEMORY;
  132. bar_res->end = bar_res->start + bar_size - 1;
  133. /*
  134. * Allocate the number of table entries needed for BAR.
  135. */
  136. while (bar_size > 0 ) {
  137. iomm_table[current_iomm_table_entry] = dev->sysdata;
  138. iobar_table[current_iomm_table_entry] = bar_num;
  139. bar_size -= IOMM_TABLE_ENTRY_SIZE;
  140. ++current_iomm_table_entry;
  141. }
  142. max_io_memory = BASE_IO_MEMORY +
  143. (IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry);
  144. spin_unlock(&iomm_table_lock);
  145. }
  146. /*
  147. * allocate_device_bars
  148. *
  149. * - Allocates ALL pci_dev BAR's and updates the resources with the
  150. * BAR value. BARS with zero length will have the resources
  151. * The HvCallPci_getBarParms is used to get the size of the BAR
  152. * space. It calls iomm_table_allocate_entry to allocate
  153. * each entry.
  154. * - Loops through The Bar resources(0 - 5) including the ROM
  155. * is resource(6).
  156. */
  157. static void allocate_device_bars(struct pci_dev *dev)
  158. {
  159. struct resource *bar_res;
  160. int bar_num;
  161. for (bar_num = 0; bar_num <= PCI_ROM_RESOURCE; ++bar_num) {
  162. bar_res = &dev->resource[bar_num];
  163. iomm_table_allocate_entry(dev, bar_num);
  164. }
  165. }
  166. /*
  167. * Log error information to system console.
  168. * Filter out the device not there errors.
  169. * PCI: EADs Connect Failed 0x18.58.10 Rc: 0x00xx
  170. * PCI: Read Vendor Failed 0x18.58.10 Rc: 0x00xx
  171. * PCI: Connect Bus Unit Failed 0x18.58.10 Rc: 0x00xx
  172. */
  173. static void pci_Log_Error(char *Error_Text, int Bus, int SubBus,
  174. int AgentId, int HvRc)
  175. {
  176. if (HvRc == 0x0302)
  177. return;
  178. printk(KERN_ERR "PCI: %s Failed: 0x%02X.%02X.%02X Rc: 0x%04X",
  179. Error_Text, Bus, SubBus, AgentId, HvRc);
  180. }
  181. /*
  182. * build_device_node(u16 Bus, int SubBus, u8 DevFn)
  183. */
  184. static struct iSeries_Device_Node *build_device_node(HvBusNumber Bus,
  185. HvSubBusNumber SubBus, int AgentId, int Function)
  186. {
  187. struct iSeries_Device_Node *node;
  188. PPCDBG(PPCDBG_BUSWALK,
  189. "-build_device_node 0x%02X.%02X.%02X Function: %02X\n",
  190. Bus, SubBus, AgentId, Function);
  191. node = kmalloc(sizeof(struct iSeries_Device_Node), GFP_KERNEL);
  192. if (node == NULL)
  193. return NULL;
  194. memset(node, 0, sizeof(struct iSeries_Device_Node));
  195. list_add_tail(&node->Device_List, &iSeries_Global_Device_List);
  196. #if 0
  197. node->DsaAddr = ((u64)Bus << 48) + ((u64)SubBus << 40) + ((u64)0x10 << 32);
  198. #endif
  199. node->DsaAddr.DsaAddr = 0;
  200. node->DsaAddr.Dsa.busNumber = Bus;
  201. node->DsaAddr.Dsa.subBusNumber = SubBus;
  202. node->DsaAddr.Dsa.deviceId = 0x10;
  203. node->AgentId = AgentId;
  204. node->DevFn = PCI_DEVFN(ISERIES_ENCODE_DEVICE(AgentId), Function);
  205. node->IoRetry = 0;
  206. iSeries_Get_Location_Code(node);
  207. return node;
  208. }
  209. /*
  210. * unsigned long __init find_and_init_phbs(void)
  211. *
  212. * Description:
  213. * This function checks for all possible system PCI host bridges that connect
  214. * PCI buses. The system hypervisor is queried as to the guest partition
  215. * ownership status. A pci_controller is built for any bus which is partially
  216. * owned or fully owned by this guest partition.
  217. */
  218. unsigned long __init find_and_init_phbs(void)
  219. {
  220. struct pci_controller *phb;
  221. HvBusNumber bus;
  222. PPCDBG(PPCDBG_BUSWALK, "find_and_init_phbs Entry\n");
  223. /* Check all possible buses. */
  224. for (bus = 0; bus < 256; bus++) {
  225. int ret = HvCallXm_testBus(bus);
  226. if (ret == 0) {
  227. printk("bus %d appears to exist\n", bus);
  228. phb = (struct pci_controller *)kmalloc(sizeof(struct pci_controller), GFP_KERNEL);
  229. if (phb == NULL)
  230. return -ENOMEM;
  231. pci_setup_pci_controller(phb);
  232. phb->pci_mem_offset = phb->local_number = bus;
  233. phb->first_busno = bus;
  234. phb->last_busno = bus;
  235. phb->ops = &iSeries_pci_ops;
  236. PPCDBG(PPCDBG_BUSWALK, "PCI:Create iSeries pci_controller(%p), Bus: %04X\n",
  237. phb, bus);
  238. /* Find and connect the devices. */
  239. scan_PHB_slots(phb);
  240. }
  241. /*
  242. * Check for Unexpected Return code, a clue that something
  243. * has gone wrong.
  244. */
  245. else if (ret != 0x0301)
  246. printk(KERN_ERR "Unexpected Return on Probe(0x%04X): 0x%04X",
  247. bus, ret);
  248. }
  249. return 0;
  250. }
  251. /*
  252. * iSeries_pcibios_init
  253. *
  254. * Chance to initialize and structures or variable before PCI Bus walk.
  255. */
  256. void iSeries_pcibios_init(void)
  257. {
  258. PPCDBG(PPCDBG_BUSWALK, "iSeries_pcibios_init Entry.\n");
  259. iomm_table_initialize();
  260. find_and_init_phbs();
  261. io_page_mask = -1;
  262. PPCDBG(PPCDBG_BUSWALK, "iSeries_pcibios_init Exit.\n");
  263. }
  264. /*
  265. * iSeries_pci_final_fixup(void)
  266. */
  267. void __init iSeries_pci_final_fixup(void)
  268. {
  269. struct pci_dev *pdev = NULL;
  270. struct iSeries_Device_Node *node;
  271. char Buffer[256];
  272. int DeviceCount = 0;
  273. PPCDBG(PPCDBG_BUSWALK, "iSeries_pcibios_fixup Entry.\n");
  274. /* Fix up at the device node and pci_dev relationship */
  275. mf_display_src(0xC9000100);
  276. printk("pcibios_final_fixup\n");
  277. for_each_pci_dev(pdev) {
  278. node = find_Device_Node(pdev->bus->number, pdev->devfn);
  279. printk("pci dev %p (%x.%x), node %p\n", pdev,
  280. pdev->bus->number, pdev->devfn, node);
  281. if (node != NULL) {
  282. ++DeviceCount;
  283. pdev->sysdata = (void *)node;
  284. node->PciDev = pdev;
  285. PPCDBG(PPCDBG_BUSWALK,
  286. "pdev 0x%p <==> DevNode 0x%p\n",
  287. pdev, node);
  288. allocate_device_bars(pdev);
  289. iSeries_Device_Information(pdev, Buffer,
  290. sizeof(Buffer));
  291. printk("%d. %s\n", DeviceCount, Buffer);
  292. iommu_devnode_init_iSeries(node);
  293. } else
  294. printk("PCI: Device Tree not found for 0x%016lX\n",
  295. (unsigned long)pdev);
  296. pdev->irq = node->Irq;
  297. }
  298. iSeries_activate_IRQs();
  299. mf_display_src(0xC9000200);
  300. }
  301. void pcibios_fixup_bus(struct pci_bus *PciBus)
  302. {
  303. PPCDBG(PPCDBG_BUSWALK, "iSeries_pcibios_fixup_bus(0x%04X) Entry.\n",
  304. PciBus->number);
  305. }
  306. void pcibios_fixup_resources(struct pci_dev *pdev)
  307. {
  308. PPCDBG(PPCDBG_BUSWALK, "fixup_resources pdev %p\n", pdev);
  309. }
  310. /*
  311. * Loop through each node function to find usable EADs bridges.
  312. */
  313. static void scan_PHB_slots(struct pci_controller *Phb)
  314. {
  315. struct HvCallPci_DeviceInfo *DevInfo;
  316. HvBusNumber bus = Phb->local_number; /* System Bus */
  317. const HvSubBusNumber SubBus = 0; /* EADs is always 0. */
  318. int HvRc = 0;
  319. int IdSel;
  320. const int MaxAgents = 8;
  321. DevInfo = (struct HvCallPci_DeviceInfo*)
  322. kmalloc(sizeof(struct HvCallPci_DeviceInfo), GFP_KERNEL);
  323. if (DevInfo == NULL)
  324. return;
  325. /*
  326. * Probe for EADs Bridges
  327. */
  328. for (IdSel = 1; IdSel < MaxAgents; ++IdSel) {
  329. HvRc = HvCallPci_getDeviceInfo(bus, SubBus, IdSel,
  330. ISERIES_HV_ADDR(DevInfo),
  331. sizeof(struct HvCallPci_DeviceInfo));
  332. if (HvRc == 0) {
  333. if (DevInfo->deviceType == HvCallPci_NodeDevice)
  334. scan_EADS_bridge(bus, SubBus, IdSel);
  335. else
  336. printk("PCI: Invalid System Configuration(0x%02X)"
  337. " for bus 0x%02x id 0x%02x.\n",
  338. DevInfo->deviceType, bus, IdSel);
  339. }
  340. else
  341. pci_Log_Error("getDeviceInfo", bus, SubBus, IdSel, HvRc);
  342. }
  343. kfree(DevInfo);
  344. }
  345. static void scan_EADS_bridge(HvBusNumber bus, HvSubBusNumber SubBus,
  346. int IdSel)
  347. {
  348. struct HvCallPci_BridgeInfo *BridgeInfo;
  349. HvAgentId AgentId;
  350. int Function;
  351. int HvRc;
  352. BridgeInfo = (struct HvCallPci_BridgeInfo *)
  353. kmalloc(sizeof(struct HvCallPci_BridgeInfo), GFP_KERNEL);
  354. if (BridgeInfo == NULL)
  355. return;
  356. /* Note: hvSubBus and irq is always be 0 at this level! */
  357. for (Function = 0; Function < 8; ++Function) {
  358. AgentId = ISERIES_PCI_AGENTID(IdSel, Function);
  359. HvRc = HvCallXm_connectBusUnit(bus, SubBus, AgentId, 0);
  360. if (HvRc == 0) {
  361. printk("found device at bus %d idsel %d func %d (AgentId %x)\n",
  362. bus, IdSel, Function, AgentId);
  363. /* Connect EADs: 0x18.00.12 = 0x00 */
  364. PPCDBG(PPCDBG_BUSWALK,
  365. "PCI:Connect EADs: 0x%02X.%02X.%02X\n",
  366. bus, SubBus, AgentId);
  367. HvRc = HvCallPci_getBusUnitInfo(bus, SubBus, AgentId,
  368. ISERIES_HV_ADDR(BridgeInfo),
  369. sizeof(struct HvCallPci_BridgeInfo));
  370. if (HvRc == 0) {
  371. printk("bridge info: type %x subbus %x maxAgents %x maxsubbus %x logslot %x\n",
  372. BridgeInfo->busUnitInfo.deviceType,
  373. BridgeInfo->subBusNumber,
  374. BridgeInfo->maxAgents,
  375. BridgeInfo->maxSubBusNumber,
  376. BridgeInfo->logicalSlotNumber);
  377. PPCDBG(PPCDBG_BUSWALK,
  378. "PCI: BridgeInfo, Type:0x%02X, SubBus:0x%02X, MaxAgents:0x%02X, MaxSubBus: 0x%02X, LSlot: 0x%02X\n",
  379. BridgeInfo->busUnitInfo.deviceType,
  380. BridgeInfo->subBusNumber,
  381. BridgeInfo->maxAgents,
  382. BridgeInfo->maxSubBusNumber,
  383. BridgeInfo->logicalSlotNumber);
  384. if (BridgeInfo->busUnitInfo.deviceType ==
  385. HvCallPci_BridgeDevice) {
  386. /* Scan_Bridge_Slot...: 0x18.00.12 */
  387. scan_bridge_slot(bus, BridgeInfo);
  388. } else
  389. printk("PCI: Invalid Bridge Configuration(0x%02X)",
  390. BridgeInfo->busUnitInfo.deviceType);
  391. }
  392. } else if (HvRc != 0x000B)
  393. pci_Log_Error("EADs Connect",
  394. bus, SubBus, AgentId, HvRc);
  395. }
  396. kfree(BridgeInfo);
  397. }
  398. /*
  399. * This assumes that the node slot is always on the primary bus!
  400. */
  401. static int scan_bridge_slot(HvBusNumber Bus,
  402. struct HvCallPci_BridgeInfo *BridgeInfo)
  403. {
  404. struct iSeries_Device_Node *node;
  405. HvSubBusNumber SubBus = BridgeInfo->subBusNumber;
  406. u16 VendorId = 0;
  407. int HvRc = 0;
  408. u8 Irq = 0;
  409. int IdSel = ISERIES_GET_DEVICE_FROM_SUBBUS(SubBus);
  410. int Function = ISERIES_GET_FUNCTION_FROM_SUBBUS(SubBus);
  411. HvAgentId EADsIdSel = ISERIES_PCI_AGENTID(IdSel, Function);
  412. /* iSeries_allocate_IRQ.: 0x18.00.12(0xA3) */
  413. Irq = iSeries_allocate_IRQ(Bus, 0, EADsIdSel);
  414. PPCDBG(PPCDBG_BUSWALK,
  415. "PCI:- allocate and assign IRQ 0x%02X.%02X.%02X = 0x%02X\n",
  416. Bus, 0, EADsIdSel, Irq);
  417. /*
  418. * Connect all functions of any device found.
  419. */
  420. for (IdSel = 1; IdSel <= BridgeInfo->maxAgents; ++IdSel) {
  421. for (Function = 0; Function < 8; ++Function) {
  422. HvAgentId AgentId = ISERIES_PCI_AGENTID(IdSel, Function);
  423. HvRc = HvCallXm_connectBusUnit(Bus, SubBus,
  424. AgentId, Irq);
  425. if (HvRc != 0) {
  426. pci_Log_Error("Connect Bus Unit",
  427. Bus, SubBus, AgentId, HvRc);
  428. continue;
  429. }
  430. HvRc = HvCallPci_configLoad16(Bus, SubBus, AgentId,
  431. PCI_VENDOR_ID, &VendorId);
  432. if (HvRc != 0) {
  433. pci_Log_Error("Read Vendor",
  434. Bus, SubBus, AgentId, HvRc);
  435. continue;
  436. }
  437. printk("read vendor ID: %x\n", VendorId);
  438. /* FoundDevice: 0x18.28.10 = 0x12AE */
  439. PPCDBG(PPCDBG_BUSWALK,
  440. "PCI:- FoundDevice: 0x%02X.%02X.%02X = 0x%04X, irq %d\n",
  441. Bus, SubBus, AgentId, VendorId, Irq);
  442. HvRc = HvCallPci_configStore8(Bus, SubBus, AgentId,
  443. PCI_INTERRUPT_LINE, Irq);
  444. if (HvRc != 0)
  445. pci_Log_Error("PciCfgStore Irq Failed!",
  446. Bus, SubBus, AgentId, HvRc);
  447. ++DeviceCount;
  448. node = build_device_node(Bus, SubBus, EADsIdSel, Function);
  449. node->Vendor = VendorId;
  450. node->Irq = Irq;
  451. node->LogicalSlot = BridgeInfo->logicalSlotNumber;
  452. } /* for (Function = 0; Function < 8; ++Function) */
  453. } /* for (IdSel = 1; IdSel <= MaxAgents; ++IdSel) */
  454. return HvRc;
  455. }
  456. /*
  457. * I/0 Memory copy MUST use mmio commands on iSeries
  458. * To do; For performance, include the hv call directly
  459. */
  460. void iSeries_memset_io(volatile void __iomem *dest, char c, size_t Count)
  461. {
  462. u8 ByteValue = c;
  463. long NumberOfBytes = Count;
  464. while (NumberOfBytes > 0) {
  465. iSeries_Write_Byte(ByteValue, dest++);
  466. -- NumberOfBytes;
  467. }
  468. }
  469. EXPORT_SYMBOL(iSeries_memset_io);
  470. void iSeries_memcpy_toio(volatile void __iomem *dest, void *source, size_t count)
  471. {
  472. char *src = source;
  473. long NumberOfBytes = count;
  474. while (NumberOfBytes > 0) {
  475. iSeries_Write_Byte(*src++, dest++);
  476. -- NumberOfBytes;
  477. }
  478. }
  479. EXPORT_SYMBOL(iSeries_memcpy_toio);
  480. void iSeries_memcpy_fromio(void *dest, const volatile void __iomem *src, size_t count)
  481. {
  482. char *dst = dest;
  483. long NumberOfBytes = count;
  484. while (NumberOfBytes > 0) {
  485. *dst++ = iSeries_Read_Byte(src++);
  486. -- NumberOfBytes;
  487. }
  488. }
  489. EXPORT_SYMBOL(iSeries_memcpy_fromio);
  490. /*
  491. * Look down the chain to find the matching Device Device
  492. */
  493. static struct iSeries_Device_Node *find_Device_Node(int bus, int devfn)
  494. {
  495. struct list_head *pos;
  496. list_for_each(pos, &iSeries_Global_Device_List) {
  497. struct iSeries_Device_Node *node =
  498. list_entry(pos, struct iSeries_Device_Node, Device_List);
  499. if ((bus == ISERIES_BUS(node)) && (devfn == node->DevFn))
  500. return node;
  501. }
  502. return NULL;
  503. }
  504. #if 0
  505. /*
  506. * Returns the device node for the passed pci_dev
  507. * Sanity Check Node PciDev to passed pci_dev
  508. * If none is found, returns a NULL which the client must handle.
  509. */
  510. static struct iSeries_Device_Node *get_Device_Node(struct pci_dev *pdev)
  511. {
  512. struct iSeries_Device_Node *node;
  513. node = pdev->sysdata;
  514. if (node == NULL || node->PciDev != pdev)
  515. node = find_Device_Node(pdev->bus->number, pdev->devfn);
  516. return node;
  517. }
  518. #endif
  519. /*
  520. * Config space read and write functions.
  521. * For now at least, we look for the device node for the bus and devfn
  522. * that we are asked to access. It may be possible to translate the devfn
  523. * to a subbus and deviceid more directly.
  524. */
  525. static u64 hv_cfg_read_func[4] = {
  526. HvCallPciConfigLoad8, HvCallPciConfigLoad16,
  527. HvCallPciConfigLoad32, HvCallPciConfigLoad32
  528. };
  529. static u64 hv_cfg_write_func[4] = {
  530. HvCallPciConfigStore8, HvCallPciConfigStore16,
  531. HvCallPciConfigStore32, HvCallPciConfigStore32
  532. };
  533. /*
  534. * Read PCI config space
  535. */
  536. static int iSeries_pci_read_config(struct pci_bus *bus, unsigned int devfn,
  537. int offset, int size, u32 *val)
  538. {
  539. struct iSeries_Device_Node *node = find_Device_Node(bus->number, devfn);
  540. u64 fn;
  541. struct HvCallPci_LoadReturn ret;
  542. if (node == NULL)
  543. return PCIBIOS_DEVICE_NOT_FOUND;
  544. if (offset > 255) {
  545. *val = ~0;
  546. return PCIBIOS_BAD_REGISTER_NUMBER;
  547. }
  548. fn = hv_cfg_read_func[(size - 1) & 3];
  549. HvCall3Ret16(fn, &ret, node->DsaAddr.DsaAddr, offset, 0);
  550. if (ret.rc != 0) {
  551. *val = ~0;
  552. return PCIBIOS_DEVICE_NOT_FOUND; /* or something */
  553. }
  554. *val = ret.value;
  555. return 0;
  556. }
  557. /*
  558. * Write PCI config space
  559. */
  560. static int iSeries_pci_write_config(struct pci_bus *bus, unsigned int devfn,
  561. int offset, int size, u32 val)
  562. {
  563. struct iSeries_Device_Node *node = find_Device_Node(bus->number, devfn);
  564. u64 fn;
  565. u64 ret;
  566. if (node == NULL)
  567. return PCIBIOS_DEVICE_NOT_FOUND;
  568. if (offset > 255)
  569. return PCIBIOS_BAD_REGISTER_NUMBER;
  570. fn = hv_cfg_write_func[(size - 1) & 3];
  571. ret = HvCall4(fn, node->DsaAddr.DsaAddr, offset, val, 0);
  572. if (ret != 0)
  573. return PCIBIOS_DEVICE_NOT_FOUND;
  574. return 0;
  575. }
  576. static struct pci_ops iSeries_pci_ops = {
  577. .read = iSeries_pci_read_config,
  578. .write = iSeries_pci_write_config
  579. };
  580. /*
  581. * Check Return Code
  582. * -> On Failure, print and log information.
  583. * Increment Retry Count, if exceeds max, panic partition.
  584. * -> If in retry, print and log success
  585. *
  586. * PCI: Device 23.90 ReadL I/O Error( 0): 0x1234
  587. * PCI: Device 23.90 ReadL Retry( 1)
  588. * PCI: Device 23.90 ReadL Retry Successful(1)
  589. */
  590. static int CheckReturnCode(char *TextHdr, struct iSeries_Device_Node *DevNode,
  591. u64 ret)
  592. {
  593. if (ret != 0) {
  594. ++Pci_Error_Count;
  595. ++DevNode->IoRetry;
  596. printk("PCI: %s: Device 0x%04X:%02X I/O Error(%2d): 0x%04X\n",
  597. TextHdr, DevNode->DsaAddr.Dsa.busNumber, DevNode->DevFn,
  598. DevNode->IoRetry, (int)ret);
  599. /*
  600. * Bump the retry and check for retry count exceeded.
  601. * If, Exceeded, panic the system.
  602. */
  603. if ((DevNode->IoRetry > Pci_Retry_Max) &&
  604. (Pci_Error_Flag > 0)) {
  605. mf_display_src(0xB6000103);
  606. panic_timeout = 0;
  607. panic("PCI: Hardware I/O Error, SRC B6000103, "
  608. "Automatic Reboot Disabled.\n");
  609. }
  610. return -1; /* Retry Try */
  611. }
  612. /* If retry was in progress, log success and rest retry count */
  613. if (DevNode->IoRetry > 0)
  614. DevNode->IoRetry = 0;
  615. return 0;
  616. }
  617. /*
  618. * Translate the I/O Address into a device node, bar, and bar offset.
  619. * Note: Make sure the passed variable end up on the stack to avoid
  620. * the exposure of being device global.
  621. */
  622. static inline struct iSeries_Device_Node *xlate_iomm_address(
  623. const volatile void __iomem *IoAddress,
  624. u64 *dsaptr, u64 *BarOffsetPtr)
  625. {
  626. unsigned long OrigIoAddr;
  627. unsigned long BaseIoAddr;
  628. unsigned long TableIndex;
  629. struct iSeries_Device_Node *DevNode;
  630. OrigIoAddr = (unsigned long __force)IoAddress;
  631. if ((OrigIoAddr < BASE_IO_MEMORY) || (OrigIoAddr >= max_io_memory))
  632. return NULL;
  633. BaseIoAddr = OrigIoAddr - BASE_IO_MEMORY;
  634. TableIndex = BaseIoAddr / IOMM_TABLE_ENTRY_SIZE;
  635. DevNode = iomm_table[TableIndex];
  636. if (DevNode != NULL) {
  637. int barnum = iobar_table[TableIndex];
  638. *dsaptr = DevNode->DsaAddr.DsaAddr | (barnum << 24);
  639. *BarOffsetPtr = BaseIoAddr % IOMM_TABLE_ENTRY_SIZE;
  640. } else
  641. panic("PCI: Invalid PCI IoAddress detected!\n");
  642. return DevNode;
  643. }
  644. /*
  645. * Read MM I/O Instructions for the iSeries
  646. * On MM I/O error, all ones are returned and iSeries_pci_IoError is cal
  647. * else, data is returned in big Endian format.
  648. *
  649. * iSeries_Read_Byte = Read Byte ( 8 bit)
  650. * iSeries_Read_Word = Read Word (16 bit)
  651. * iSeries_Read_Long = Read Long (32 bit)
  652. */
  653. u8 iSeries_Read_Byte(const volatile void __iomem *IoAddress)
  654. {
  655. u64 BarOffset;
  656. u64 dsa;
  657. struct HvCallPci_LoadReturn ret;
  658. struct iSeries_Device_Node *DevNode =
  659. xlate_iomm_address(IoAddress, &dsa, &BarOffset);
  660. if (DevNode == NULL) {
  661. static unsigned long last_jiffies;
  662. static int num_printed;
  663. if ((jiffies - last_jiffies) > 60 * HZ) {
  664. last_jiffies = jiffies;
  665. num_printed = 0;
  666. }
  667. if (num_printed++ < 10)
  668. printk(KERN_ERR "iSeries_Read_Byte: invalid access at IO address %p\n", IoAddress);
  669. return 0xff;
  670. }
  671. do {
  672. ++Pci_Io_Read_Count;
  673. HvCall3Ret16(HvCallPciBarLoad8, &ret, dsa, BarOffset, 0);
  674. } while (CheckReturnCode("RDB", DevNode, ret.rc) != 0);
  675. return (u8)ret.value;
  676. }
  677. EXPORT_SYMBOL(iSeries_Read_Byte);
  678. u16 iSeries_Read_Word(const volatile void __iomem *IoAddress)
  679. {
  680. u64 BarOffset;
  681. u64 dsa;
  682. struct HvCallPci_LoadReturn ret;
  683. struct iSeries_Device_Node *DevNode =
  684. xlate_iomm_address(IoAddress, &dsa, &BarOffset);
  685. if (DevNode == NULL) {
  686. static unsigned long last_jiffies;
  687. static int num_printed;
  688. if ((jiffies - last_jiffies) > 60 * HZ) {
  689. last_jiffies = jiffies;
  690. num_printed = 0;
  691. }
  692. if (num_printed++ < 10)
  693. printk(KERN_ERR "iSeries_Read_Word: invalid access at IO address %p\n", IoAddress);
  694. return 0xffff;
  695. }
  696. do {
  697. ++Pci_Io_Read_Count;
  698. HvCall3Ret16(HvCallPciBarLoad16, &ret, dsa,
  699. BarOffset, 0);
  700. } while (CheckReturnCode("RDW", DevNode, ret.rc) != 0);
  701. return swab16((u16)ret.value);
  702. }
  703. EXPORT_SYMBOL(iSeries_Read_Word);
  704. u32 iSeries_Read_Long(const volatile void __iomem *IoAddress)
  705. {
  706. u64 BarOffset;
  707. u64 dsa;
  708. struct HvCallPci_LoadReturn ret;
  709. struct iSeries_Device_Node *DevNode =
  710. xlate_iomm_address(IoAddress, &dsa, &BarOffset);
  711. if (DevNode == NULL) {
  712. static unsigned long last_jiffies;
  713. static int num_printed;
  714. if ((jiffies - last_jiffies) > 60 * HZ) {
  715. last_jiffies = jiffies;
  716. num_printed = 0;
  717. }
  718. if (num_printed++ < 10)
  719. printk(KERN_ERR "iSeries_Read_Long: invalid access at IO address %p\n", IoAddress);
  720. return 0xffffffff;
  721. }
  722. do {
  723. ++Pci_Io_Read_Count;
  724. HvCall3Ret16(HvCallPciBarLoad32, &ret, dsa,
  725. BarOffset, 0);
  726. } while (CheckReturnCode("RDL", DevNode, ret.rc) != 0);
  727. return swab32((u32)ret.value);
  728. }
  729. EXPORT_SYMBOL(iSeries_Read_Long);
  730. /*
  731. * Write MM I/O Instructions for the iSeries
  732. *
  733. * iSeries_Write_Byte = Write Byte (8 bit)
  734. * iSeries_Write_Word = Write Word(16 bit)
  735. * iSeries_Write_Long = Write Long(32 bit)
  736. */
  737. void iSeries_Write_Byte(u8 data, volatile void __iomem *IoAddress)
  738. {
  739. u64 BarOffset;
  740. u64 dsa;
  741. u64 rc;
  742. struct iSeries_Device_Node *DevNode =
  743. xlate_iomm_address(IoAddress, &dsa, &BarOffset);
  744. if (DevNode == NULL) {
  745. static unsigned long last_jiffies;
  746. static int num_printed;
  747. if ((jiffies - last_jiffies) > 60 * HZ) {
  748. last_jiffies = jiffies;
  749. num_printed = 0;
  750. }
  751. if (num_printed++ < 10)
  752. printk(KERN_ERR "iSeries_Write_Byte: invalid access at IO address %p\n", IoAddress);
  753. return;
  754. }
  755. do {
  756. ++Pci_Io_Write_Count;
  757. rc = HvCall4(HvCallPciBarStore8, dsa, BarOffset, data, 0);
  758. } while (CheckReturnCode("WWB", DevNode, rc) != 0);
  759. }
  760. EXPORT_SYMBOL(iSeries_Write_Byte);
  761. void iSeries_Write_Word(u16 data, volatile void __iomem *IoAddress)
  762. {
  763. u64 BarOffset;
  764. u64 dsa;
  765. u64 rc;
  766. struct iSeries_Device_Node *DevNode =
  767. xlate_iomm_address(IoAddress, &dsa, &BarOffset);
  768. if (DevNode == NULL) {
  769. static unsigned long last_jiffies;
  770. static int num_printed;
  771. if ((jiffies - last_jiffies) > 60 * HZ) {
  772. last_jiffies = jiffies;
  773. num_printed = 0;
  774. }
  775. if (num_printed++ < 10)
  776. printk(KERN_ERR "iSeries_Write_Word: invalid access at IO address %p\n", IoAddress);
  777. return;
  778. }
  779. do {
  780. ++Pci_Io_Write_Count;
  781. rc = HvCall4(HvCallPciBarStore16, dsa, BarOffset, swab16(data), 0);
  782. } while (CheckReturnCode("WWW", DevNode, rc) != 0);
  783. }
  784. EXPORT_SYMBOL(iSeries_Write_Word);
  785. void iSeries_Write_Long(u32 data, volatile void __iomem *IoAddress)
  786. {
  787. u64 BarOffset;
  788. u64 dsa;
  789. u64 rc;
  790. struct iSeries_Device_Node *DevNode =
  791. xlate_iomm_address(IoAddress, &dsa, &BarOffset);
  792. if (DevNode == NULL) {
  793. static unsigned long last_jiffies;
  794. static int num_printed;
  795. if ((jiffies - last_jiffies) > 60 * HZ) {
  796. last_jiffies = jiffies;
  797. num_printed = 0;
  798. }
  799. if (num_printed++ < 10)
  800. printk(KERN_ERR "iSeries_Write_Long: invalid access at IO address %p\n", IoAddress);
  801. return;
  802. }
  803. do {
  804. ++Pci_Io_Write_Count;
  805. rc = HvCall4(HvCallPciBarStore32, dsa, BarOffset, swab32(data), 0);
  806. } while (CheckReturnCode("WWL", DevNode, rc) != 0);
  807. }
  808. EXPORT_SYMBOL(iSeries_Write_Long);