pci.c 24 KB

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