drivers.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * drivers.c
  4. *
  5. * Copyright (c) 1999 The Puffin Group
  6. * Copyright (c) 2001 Matthew Wilcox for Hewlett Packard
  7. * Copyright (c) 2001 Helge Deller <[email protected]>
  8. * Copyright (c) 2001,2002 Ryan Bradetich
  9. * Copyright (c) 2004-2005 Thibaut VARENE <[email protected]>
  10. *
  11. * The file handles registering devices and drivers, then matching them.
  12. * It's the closest we get to a dating agency.
  13. *
  14. * If you're thinking about modifying this file, here are some gotchas to
  15. * bear in mind:
  16. * - 715/Mirage device paths have a dummy device between Lasi and its children
  17. * - The EISA adapter may show up as a sibling or child of Wax
  18. * - Dino has an optionally functional serial port. If firmware enables it,
  19. * it shows up as a child of Dino. If firmware disables it, the buswalk
  20. * finds it and it shows up as a child of Cujo
  21. * - Dino has both parisc and pci devices as children
  22. * - parisc devices are discovered in a random order, including children
  23. * before parents in some cases.
  24. */
  25. #include <linux/slab.h>
  26. #include <linux/types.h>
  27. #include <linux/kernel.h>
  28. #include <linux/pci.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/string.h>
  31. #include <linux/export.h>
  32. #include <linux/dma-map-ops.h>
  33. #include <asm/hardware.h>
  34. #include <asm/io.h>
  35. #include <asm/pdc.h>
  36. #include <asm/parisc-device.h>
  37. #include <asm/ropes.h>
  38. /* See comments in include/asm-parisc/pci.h */
  39. const struct dma_map_ops *hppa_dma_ops __ro_after_init;
  40. EXPORT_SYMBOL(hppa_dma_ops);
  41. static struct device root = {
  42. .init_name = "parisc",
  43. };
  44. static inline int check_dev(struct device *dev)
  45. {
  46. if (dev->bus == &parisc_bus_type) {
  47. struct parisc_device *pdev;
  48. pdev = to_parisc_device(dev);
  49. return pdev->id.hw_type != HPHW_FAULTY;
  50. }
  51. return 1;
  52. }
  53. static struct device *
  54. parse_tree_node(struct device *parent, int index, struct hardware_path *modpath);
  55. struct recurse_struct {
  56. void * obj;
  57. int (*fn)(struct device *, void *);
  58. };
  59. static int descend_children(struct device * dev, void * data)
  60. {
  61. struct recurse_struct * recurse_data = (struct recurse_struct *)data;
  62. if (recurse_data->fn(dev, recurse_data->obj))
  63. return 1;
  64. else
  65. return device_for_each_child(dev, recurse_data, descend_children);
  66. }
  67. /**
  68. * for_each_padev - Iterate over all devices in the tree
  69. * @fn: Function to call for each device.
  70. * @data: Data to pass to the called function.
  71. *
  72. * This performs a depth-first traversal of the tree, calling the
  73. * function passed for each node. It calls the function for parents
  74. * before children.
  75. */
  76. static int for_each_padev(int (*fn)(struct device *, void *), void * data)
  77. {
  78. struct recurse_struct recurse_data = {
  79. .obj = data,
  80. .fn = fn,
  81. };
  82. return device_for_each_child(&root, &recurse_data, descend_children);
  83. }
  84. /**
  85. * match_device - Report whether this driver can handle this device
  86. * @driver: the PA-RISC driver to try
  87. * @dev: the PA-RISC device to try
  88. */
  89. static int match_device(struct parisc_driver *driver, struct parisc_device *dev)
  90. {
  91. const struct parisc_device_id *ids;
  92. for (ids = driver->id_table; ids->sversion; ids++) {
  93. if ((ids->sversion != SVERSION_ANY_ID) &&
  94. (ids->sversion != dev->id.sversion))
  95. continue;
  96. if ((ids->hw_type != HWTYPE_ANY_ID) &&
  97. (ids->hw_type != dev->id.hw_type))
  98. continue;
  99. if ((ids->hversion != HVERSION_ANY_ID) &&
  100. (ids->hversion != dev->id.hversion))
  101. continue;
  102. return 1;
  103. }
  104. return 0;
  105. }
  106. static int parisc_driver_probe(struct device *dev)
  107. {
  108. int rc;
  109. struct parisc_device *pa_dev = to_parisc_device(dev);
  110. struct parisc_driver *pa_drv = to_parisc_driver(dev->driver);
  111. rc = pa_drv->probe(pa_dev);
  112. if (!rc)
  113. pa_dev->driver = pa_drv;
  114. return rc;
  115. }
  116. static void __exit parisc_driver_remove(struct device *dev)
  117. {
  118. struct parisc_device *pa_dev = to_parisc_device(dev);
  119. struct parisc_driver *pa_drv = to_parisc_driver(dev->driver);
  120. if (pa_drv->remove)
  121. pa_drv->remove(pa_dev);
  122. }
  123. /**
  124. * register_parisc_driver - Register this driver if it can handle a device
  125. * @driver: the PA-RISC driver to try
  126. */
  127. int register_parisc_driver(struct parisc_driver *driver)
  128. {
  129. /* FIXME: we need this because apparently the sti
  130. * driver can be registered twice */
  131. if (driver->drv.name) {
  132. pr_warn("BUG: skipping previously registered driver %s\n",
  133. driver->name);
  134. return 1;
  135. }
  136. if (!driver->probe) {
  137. pr_warn("BUG: driver %s has no probe routine\n", driver->name);
  138. return 1;
  139. }
  140. driver->drv.bus = &parisc_bus_type;
  141. /* We install our own probe and remove routines */
  142. WARN_ON(driver->drv.probe != NULL);
  143. WARN_ON(driver->drv.remove != NULL);
  144. driver->drv.name = driver->name;
  145. return driver_register(&driver->drv);
  146. }
  147. EXPORT_SYMBOL(register_parisc_driver);
  148. struct match_count {
  149. struct parisc_driver * driver;
  150. int count;
  151. };
  152. static int match_and_count(struct device * dev, void * data)
  153. {
  154. struct match_count * m = data;
  155. struct parisc_device * pdev = to_parisc_device(dev);
  156. if (check_dev(dev)) {
  157. if (match_device(m->driver, pdev))
  158. m->count++;
  159. }
  160. return 0;
  161. }
  162. /**
  163. * count_parisc_driver - count # of devices this driver would match
  164. * @driver: the PA-RISC driver to try
  165. *
  166. * Use by IOMMU support to "guess" the right size IOPdir.
  167. * Formula is something like memsize/(num_iommu * entry_size).
  168. */
  169. int __init count_parisc_driver(struct parisc_driver *driver)
  170. {
  171. struct match_count m = {
  172. .driver = driver,
  173. .count = 0,
  174. };
  175. for_each_padev(match_and_count, &m);
  176. return m.count;
  177. }
  178. /**
  179. * unregister_parisc_driver - Unregister this driver from the list of drivers
  180. * @driver: the PA-RISC driver to unregister
  181. */
  182. int unregister_parisc_driver(struct parisc_driver *driver)
  183. {
  184. driver_unregister(&driver->drv);
  185. return 0;
  186. }
  187. EXPORT_SYMBOL(unregister_parisc_driver);
  188. struct find_data {
  189. unsigned long hpa;
  190. struct parisc_device * dev;
  191. };
  192. static int find_device(struct device * dev, void * data)
  193. {
  194. struct parisc_device * pdev = to_parisc_device(dev);
  195. struct find_data * d = (struct find_data*)data;
  196. if (check_dev(dev)) {
  197. if (pdev->hpa.start == d->hpa) {
  198. d->dev = pdev;
  199. return 1;
  200. }
  201. }
  202. return 0;
  203. }
  204. static struct parisc_device *find_device_by_addr(unsigned long hpa)
  205. {
  206. struct find_data d = {
  207. .hpa = hpa,
  208. };
  209. int ret;
  210. ret = for_each_padev(find_device, &d);
  211. return ret ? d.dev : NULL;
  212. }
  213. static int __init is_IKE_device(struct device *dev, void *data)
  214. {
  215. struct parisc_device *pdev = to_parisc_device(dev);
  216. if (!check_dev(dev))
  217. return 0;
  218. if (pdev->id.hw_type != HPHW_BCPORT)
  219. return 0;
  220. if (IS_IKE(pdev) ||
  221. (pdev->id.hversion == REO_MERCED_PORT) ||
  222. (pdev->id.hversion == REOG_MERCED_PORT)) {
  223. return 1;
  224. }
  225. return 0;
  226. }
  227. int __init machine_has_merced_bus(void)
  228. {
  229. int ret;
  230. ret = for_each_padev(is_IKE_device, NULL);
  231. return ret ? 1 : 0;
  232. }
  233. /**
  234. * find_pa_parent_type - Find a parent of a specific type
  235. * @dev: The device to start searching from
  236. * @type: The device type to search for.
  237. *
  238. * Walks up the device tree looking for a device of the specified type.
  239. * If it finds it, it returns it. If not, it returns NULL.
  240. */
  241. const struct parisc_device *
  242. find_pa_parent_type(const struct parisc_device *padev, int type)
  243. {
  244. const struct device *dev = &padev->dev;
  245. while (dev != &root) {
  246. struct parisc_device *candidate = to_parisc_device(dev);
  247. if (candidate->id.hw_type == type)
  248. return candidate;
  249. dev = dev->parent;
  250. }
  251. return NULL;
  252. }
  253. /*
  254. * get_node_path fills in @path with the firmware path to the device.
  255. * Note that if @node is a parisc device, we don't fill in the 'mod' field.
  256. * This is because both callers pass the parent and fill in the mod
  257. * themselves. If @node is a PCI device, we do fill it in, even though this
  258. * is inconsistent.
  259. */
  260. static void get_node_path(struct device *dev, struct hardware_path *path)
  261. {
  262. int i = 5;
  263. memset(&path->bc, -1, 6);
  264. if (dev_is_pci(dev)) {
  265. unsigned int devfn = to_pci_dev(dev)->devfn;
  266. path->mod = PCI_FUNC(devfn);
  267. path->bc[i--] = PCI_SLOT(devfn);
  268. dev = dev->parent;
  269. }
  270. while (dev != &root) {
  271. if (dev_is_pci(dev)) {
  272. unsigned int devfn = to_pci_dev(dev)->devfn;
  273. path->bc[i--] = PCI_SLOT(devfn) | (PCI_FUNC(devfn)<< 5);
  274. } else if (dev->bus == &parisc_bus_type) {
  275. path->bc[i--] = to_parisc_device(dev)->hw_path;
  276. }
  277. dev = dev->parent;
  278. }
  279. }
  280. static char *print_hwpath(struct hardware_path *path, char *output)
  281. {
  282. int i;
  283. for (i = 0; i < 6; i++) {
  284. if (path->bc[i] == -1)
  285. continue;
  286. output += sprintf(output, "%u/", (unsigned char) path->bc[i]);
  287. }
  288. output += sprintf(output, "%u", (unsigned char) path->mod);
  289. return output;
  290. }
  291. /**
  292. * print_pa_hwpath - Returns hardware path for PA devices
  293. * dev: The device to return the path for
  294. * output: Pointer to a previously-allocated array to place the path in.
  295. *
  296. * This function fills in the output array with a human-readable path
  297. * to a PA device. This string is compatible with that used by PDC, and
  298. * may be printed on the outside of the box.
  299. */
  300. char *print_pa_hwpath(struct parisc_device *dev, char *output)
  301. {
  302. struct hardware_path path;
  303. get_node_path(dev->dev.parent, &path);
  304. path.mod = dev->hw_path;
  305. return print_hwpath(&path, output);
  306. }
  307. EXPORT_SYMBOL(print_pa_hwpath);
  308. #if defined(CONFIG_PCI) || defined(CONFIG_ISA)
  309. /**
  310. * get_pci_node_path - Determines the hardware path for a PCI device
  311. * @pdev: The device to return the path for
  312. * @path: Pointer to a previously-allocated array to place the path in.
  313. *
  314. * This function fills in the hardware_path structure with the route to
  315. * the specified PCI device. This structure is suitable for passing to
  316. * PDC calls.
  317. */
  318. void get_pci_node_path(struct pci_dev *pdev, struct hardware_path *path)
  319. {
  320. get_node_path(&pdev->dev, path);
  321. }
  322. EXPORT_SYMBOL(get_pci_node_path);
  323. /**
  324. * print_pci_hwpath - Returns hardware path for PCI devices
  325. * dev: The device to return the path for
  326. * output: Pointer to a previously-allocated array to place the path in.
  327. *
  328. * This function fills in the output array with a human-readable path
  329. * to a PCI device. This string is compatible with that used by PDC, and
  330. * may be printed on the outside of the box.
  331. */
  332. char *print_pci_hwpath(struct pci_dev *dev, char *output)
  333. {
  334. struct hardware_path path;
  335. get_pci_node_path(dev, &path);
  336. return print_hwpath(&path, output);
  337. }
  338. EXPORT_SYMBOL(print_pci_hwpath);
  339. #endif /* defined(CONFIG_PCI) || defined(CONFIG_ISA) */
  340. static void setup_bus_id(struct parisc_device *padev)
  341. {
  342. struct hardware_path path;
  343. char name[28];
  344. char *output = name;
  345. int i;
  346. get_node_path(padev->dev.parent, &path);
  347. for (i = 0; i < 6; i++) {
  348. if (path.bc[i] == -1)
  349. continue;
  350. output += sprintf(output, "%u:", (unsigned char) path.bc[i]);
  351. }
  352. sprintf(output, "%u", (unsigned char) padev->hw_path);
  353. dev_set_name(&padev->dev, name);
  354. }
  355. struct parisc_device * __init create_tree_node(char id, struct device *parent)
  356. {
  357. struct parisc_device *dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  358. if (!dev)
  359. return NULL;
  360. dev->hw_path = id;
  361. dev->id.hw_type = HPHW_FAULTY;
  362. dev->dev.parent = parent;
  363. setup_bus_id(dev);
  364. dev->dev.bus = &parisc_bus_type;
  365. dev->dma_mask = 0xffffffffUL; /* PARISC devices are 32-bit */
  366. /* make the generic dma mask a pointer to the parisc one */
  367. dev->dev.dma_mask = &dev->dma_mask;
  368. dev->dev.coherent_dma_mask = dev->dma_mask;
  369. if (device_register(&dev->dev)) {
  370. kfree(dev);
  371. return NULL;
  372. }
  373. return dev;
  374. }
  375. struct match_id_data {
  376. char id;
  377. struct parisc_device * dev;
  378. };
  379. static int match_by_id(struct device * dev, void * data)
  380. {
  381. struct parisc_device * pdev = to_parisc_device(dev);
  382. struct match_id_data * d = data;
  383. if (pdev->hw_path == d->id) {
  384. d->dev = pdev;
  385. return 1;
  386. }
  387. return 0;
  388. }
  389. /**
  390. * alloc_tree_node - returns a device entry in the iotree
  391. * @parent: the parent node in the tree
  392. * @id: the element of the module path for this entry
  393. *
  394. * Checks all the children of @parent for a matching @id. If none
  395. * found, it allocates a new device and returns it.
  396. */
  397. static struct parisc_device * __init alloc_tree_node(
  398. struct device *parent, char id)
  399. {
  400. struct match_id_data d = {
  401. .id = id,
  402. };
  403. if (device_for_each_child(parent, &d, match_by_id))
  404. return d.dev;
  405. else
  406. return create_tree_node(id, parent);
  407. }
  408. static struct parisc_device *create_parisc_device(struct hardware_path *modpath)
  409. {
  410. int i;
  411. struct device *parent = &root;
  412. for (i = 0; i < 6; i++) {
  413. if (modpath->bc[i] == -1)
  414. continue;
  415. parent = &alloc_tree_node(parent, modpath->bc[i])->dev;
  416. }
  417. return alloc_tree_node(parent, modpath->mod);
  418. }
  419. struct parisc_device * __init
  420. alloc_pa_dev(unsigned long hpa, struct hardware_path *mod_path)
  421. {
  422. int status;
  423. unsigned long bytecnt;
  424. u8 iodc_data[32];
  425. struct parisc_device *dev;
  426. const char *name;
  427. /* Check to make sure this device has not already been added - Ryan */
  428. if (find_device_by_addr(hpa) != NULL)
  429. return NULL;
  430. status = pdc_iodc_read(&bytecnt, hpa, 0, &iodc_data, 32);
  431. if (status != PDC_OK)
  432. return NULL;
  433. dev = create_parisc_device(mod_path);
  434. if (dev->id.hw_type != HPHW_FAULTY) {
  435. pr_err("Two devices have hardware path [%s]. IODC data for second device: %7phN\n"
  436. "Rearranging GSC cards sometimes helps\n",
  437. parisc_pathname(dev), iodc_data);
  438. return NULL;
  439. }
  440. dev->id.hw_type = iodc_data[3] & 0x1f;
  441. dev->id.hversion = (iodc_data[0] << 4) | ((iodc_data[1] & 0xf0) >> 4);
  442. dev->id.hversion_rev = iodc_data[1] & 0x0f;
  443. dev->id.sversion = ((iodc_data[4] & 0x0f) << 16) |
  444. (iodc_data[5] << 8) | iodc_data[6];
  445. dev->hpa.start = hpa;
  446. /* This is awkward. The STI spec says that gfx devices may occupy
  447. * 32MB or 64MB. Unfortunately, we don't know how to tell whether
  448. * it's the former or the latter. Assumptions either way can hurt us.
  449. */
  450. if (hpa == 0xf4000000 || hpa == 0xf8000000) {
  451. dev->hpa.end = hpa + 0x03ffffff;
  452. } else if (hpa == 0xf6000000 || hpa == 0xfa000000) {
  453. dev->hpa.end = hpa + 0x01ffffff;
  454. } else {
  455. dev->hpa.end = hpa + 0xfff;
  456. }
  457. dev->hpa.flags = IORESOURCE_MEM;
  458. dev->hpa.name = dev->name;
  459. name = parisc_hardware_description(&dev->id) ? : "unknown";
  460. snprintf(dev->name, sizeof(dev->name), "%s [%s]",
  461. name, parisc_pathname(dev));
  462. /* Silently fail things like mouse ports which are subsumed within
  463. * the keyboard controller
  464. */
  465. if ((hpa & 0xfff) == 0 && insert_resource(&iomem_resource, &dev->hpa))
  466. pr_warn("Unable to claim HPA %lx for device %s\n", hpa, name);
  467. return dev;
  468. }
  469. static int parisc_generic_match(struct device *dev, struct device_driver *drv)
  470. {
  471. return match_device(to_parisc_driver(drv), to_parisc_device(dev));
  472. }
  473. static ssize_t make_modalias(struct device *dev, char *buf)
  474. {
  475. const struct parisc_device *padev = to_parisc_device(dev);
  476. const struct parisc_device_id *id = &padev->id;
  477. return sprintf(buf, "parisc:t%02Xhv%04Xrev%02Xsv%08X\n",
  478. (u8)id->hw_type, (u16)id->hversion, (u8)id->hversion_rev,
  479. (u32)id->sversion);
  480. }
  481. static int parisc_uevent(struct device *dev, struct kobj_uevent_env *env)
  482. {
  483. const struct parisc_device *padev;
  484. char modalias[40];
  485. if (!dev)
  486. return -ENODEV;
  487. padev = to_parisc_device(dev);
  488. if (!padev)
  489. return -ENODEV;
  490. if (add_uevent_var(env, "PARISC_NAME=%s", padev->name))
  491. return -ENOMEM;
  492. make_modalias(dev, modalias);
  493. if (add_uevent_var(env, "MODALIAS=%s", modalias))
  494. return -ENOMEM;
  495. return 0;
  496. }
  497. #define pa_dev_attr(name, field, format_string) \
  498. static ssize_t name##_show(struct device *dev, struct device_attribute *attr, char *buf) \
  499. { \
  500. struct parisc_device *padev = to_parisc_device(dev); \
  501. return sprintf(buf, format_string, padev->field); \
  502. } \
  503. static DEVICE_ATTR_RO(name);
  504. #define pa_dev_attr_id(field, format) pa_dev_attr(field, id.field, format)
  505. pa_dev_attr(irq, irq, "%u\n");
  506. pa_dev_attr_id(hw_type, "0x%02x\n");
  507. pa_dev_attr(rev, id.hversion_rev, "0x%x\n");
  508. pa_dev_attr_id(hversion, "0x%03x\n");
  509. pa_dev_attr_id(sversion, "0x%05x\n");
  510. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
  511. {
  512. return make_modalias(dev, buf);
  513. }
  514. static DEVICE_ATTR_RO(modalias);
  515. static struct attribute *parisc_device_attrs[] = {
  516. &dev_attr_irq.attr,
  517. &dev_attr_hw_type.attr,
  518. &dev_attr_rev.attr,
  519. &dev_attr_hversion.attr,
  520. &dev_attr_sversion.attr,
  521. &dev_attr_modalias.attr,
  522. NULL,
  523. };
  524. ATTRIBUTE_GROUPS(parisc_device);
  525. struct bus_type parisc_bus_type = {
  526. .name = "parisc",
  527. .match = parisc_generic_match,
  528. .uevent = parisc_uevent,
  529. .dev_groups = parisc_device_groups,
  530. .probe = parisc_driver_probe,
  531. .remove = __exit_p(parisc_driver_remove),
  532. };
  533. /**
  534. * register_parisc_device - Locate a driver to manage this device.
  535. * @dev: The parisc device.
  536. *
  537. * Search the driver list for a driver that is willing to manage
  538. * this device.
  539. */
  540. int __init register_parisc_device(struct parisc_device *dev)
  541. {
  542. if (!dev)
  543. return 0;
  544. if (dev->driver)
  545. return 1;
  546. return 0;
  547. }
  548. /**
  549. * match_pci_device - Matches a pci device against a given hardware path
  550. * entry.
  551. * @dev: the generic device (known to be contained by a pci_dev).
  552. * @index: the current BC index
  553. * @modpath: the hardware path.
  554. * @return: true if the device matches the hardware path.
  555. */
  556. static int match_pci_device(struct device *dev, int index,
  557. struct hardware_path *modpath)
  558. {
  559. struct pci_dev *pdev = to_pci_dev(dev);
  560. int id;
  561. if (index == 5) {
  562. /* we are at the end of the path, and on the actual device */
  563. unsigned int devfn = pdev->devfn;
  564. return ((modpath->bc[5] == PCI_SLOT(devfn)) &&
  565. (modpath->mod == PCI_FUNC(devfn)));
  566. }
  567. /* index might be out of bounds for bc[] */
  568. if (index >= 6)
  569. return 0;
  570. id = PCI_SLOT(pdev->devfn) | (PCI_FUNC(pdev->devfn) << 5);
  571. return (modpath->bc[index] == id);
  572. }
  573. /**
  574. * match_parisc_device - Matches a parisc device against a given hardware
  575. * path entry.
  576. * @dev: the generic device (known to be contained by a parisc_device).
  577. * @index: the current BC index
  578. * @modpath: the hardware path.
  579. * @return: true if the device matches the hardware path.
  580. */
  581. static int match_parisc_device(struct device *dev, int index,
  582. struct hardware_path *modpath)
  583. {
  584. struct parisc_device *curr = to_parisc_device(dev);
  585. char id = (index == 6) ? modpath->mod : modpath->bc[index];
  586. return (curr->hw_path == id);
  587. }
  588. struct parse_tree_data {
  589. int index;
  590. struct hardware_path * modpath;
  591. struct device * dev;
  592. };
  593. static int check_parent(struct device * dev, void * data)
  594. {
  595. struct parse_tree_data * d = data;
  596. if (check_dev(dev)) {
  597. if (dev->bus == &parisc_bus_type) {
  598. if (match_parisc_device(dev, d->index, d->modpath))
  599. d->dev = dev;
  600. } else if (dev_is_pci(dev)) {
  601. if (match_pci_device(dev, d->index, d->modpath))
  602. d->dev = dev;
  603. } else if (dev->bus == NULL) {
  604. /* we are on a bus bridge */
  605. struct device *new = parse_tree_node(dev, d->index, d->modpath);
  606. if (new)
  607. d->dev = new;
  608. }
  609. }
  610. return d->dev != NULL;
  611. }
  612. /**
  613. * parse_tree_node - returns a device entry in the iotree
  614. * @parent: the parent node in the tree
  615. * @index: the current BC index
  616. * @modpath: the hardware_path struct to match a device against
  617. * @return: The corresponding device if found, NULL otherwise.
  618. *
  619. * Checks all the children of @parent for a matching @id. If none
  620. * found, it returns NULL.
  621. */
  622. static struct device *
  623. parse_tree_node(struct device *parent, int index, struct hardware_path *modpath)
  624. {
  625. struct parse_tree_data d = {
  626. .index = index,
  627. .modpath = modpath,
  628. };
  629. struct recurse_struct recurse_data = {
  630. .obj = &d,
  631. .fn = check_parent,
  632. };
  633. if (device_for_each_child(parent, &recurse_data, descend_children))
  634. /* nothing */;
  635. return d.dev;
  636. }
  637. /**
  638. * hwpath_to_device - Finds the generic device corresponding to a given hardware path.
  639. * @modpath: the hardware path.
  640. * @return: The target device, NULL if not found.
  641. */
  642. struct device *hwpath_to_device(struct hardware_path *modpath)
  643. {
  644. int i;
  645. struct device *parent = &root;
  646. for (i = 0; i < 6; i++) {
  647. if (modpath->bc[i] == -1)
  648. continue;
  649. parent = parse_tree_node(parent, i, modpath);
  650. if (!parent)
  651. return NULL;
  652. }
  653. if (dev_is_pci(parent)) /* pci devices already parse MOD */
  654. return parent;
  655. else
  656. return parse_tree_node(parent, 6, modpath);
  657. }
  658. EXPORT_SYMBOL(hwpath_to_device);
  659. /**
  660. * device_to_hwpath - Populates the hwpath corresponding to the given device.
  661. * @param dev the target device
  662. * @param path pointer to a previously allocated hwpath struct to be filled in
  663. */
  664. void device_to_hwpath(struct device *dev, struct hardware_path *path)
  665. {
  666. struct parisc_device *padev;
  667. if (dev->bus == &parisc_bus_type) {
  668. padev = to_parisc_device(dev);
  669. get_node_path(dev->parent, path);
  670. path->mod = padev->hw_path;
  671. } else if (dev_is_pci(dev)) {
  672. get_node_path(dev, path);
  673. }
  674. }
  675. EXPORT_SYMBOL(device_to_hwpath);
  676. #define BC_PORT_MASK 0x8
  677. #define BC_LOWER_PORT 0x8
  678. #define BUS_CONVERTER(dev) \
  679. ((dev->id.hw_type == HPHW_IOA) || (dev->id.hw_type == HPHW_BCPORT))
  680. #define IS_LOWER_PORT(dev) \
  681. ((gsc_readl(dev->hpa.start + offsetof(struct bc_module, io_status)) \
  682. & BC_PORT_MASK) == BC_LOWER_PORT)
  683. #define MAX_NATIVE_DEVICES 64
  684. #define NATIVE_DEVICE_OFFSET 0x1000
  685. #define FLEX_MASK F_EXTEND(0xfffc0000)
  686. #define IO_IO_LOW offsetof(struct bc_module, io_io_low)
  687. #define IO_IO_HIGH offsetof(struct bc_module, io_io_high)
  688. #define READ_IO_IO_LOW(dev) (unsigned long)(signed int)gsc_readl(dev->hpa.start + IO_IO_LOW)
  689. #define READ_IO_IO_HIGH(dev) (unsigned long)(signed int)gsc_readl(dev->hpa.start + IO_IO_HIGH)
  690. static void walk_native_bus(unsigned long io_io_low, unsigned long io_io_high,
  691. struct device *parent);
  692. static void __init walk_lower_bus(struct parisc_device *dev)
  693. {
  694. unsigned long io_io_low, io_io_high;
  695. if (!BUS_CONVERTER(dev) || IS_LOWER_PORT(dev))
  696. return;
  697. if (dev->id.hw_type == HPHW_IOA) {
  698. io_io_low = (unsigned long)(signed int)(READ_IO_IO_LOW(dev) << 16);
  699. io_io_high = io_io_low + MAX_NATIVE_DEVICES * NATIVE_DEVICE_OFFSET;
  700. } else {
  701. io_io_low = (READ_IO_IO_LOW(dev) + ~FLEX_MASK) & FLEX_MASK;
  702. io_io_high = (READ_IO_IO_HIGH(dev)+ ~FLEX_MASK) & FLEX_MASK;
  703. }
  704. walk_native_bus(io_io_low, io_io_high, &dev->dev);
  705. }
  706. /**
  707. * walk_native_bus -- Probe a bus for devices
  708. * @io_io_low: Base address of this bus.
  709. * @io_io_high: Last address of this bus.
  710. * @parent: The parent bus device.
  711. *
  712. * A native bus (eg Runway or GSC) may have up to 64 devices on it,
  713. * spaced at intervals of 0x1000 bytes. PDC may not inform us of these
  714. * devices, so we have to probe for them. Unfortunately, we may find
  715. * devices which are not physically connected (such as extra serial &
  716. * keyboard ports). This problem is not yet solved.
  717. */
  718. static void __init walk_native_bus(unsigned long io_io_low,
  719. unsigned long io_io_high, struct device *parent)
  720. {
  721. int i, devices_found = 0;
  722. unsigned long hpa = io_io_low;
  723. struct hardware_path path;
  724. get_node_path(parent, &path);
  725. do {
  726. for(i = 0; i < MAX_NATIVE_DEVICES; i++, hpa += NATIVE_DEVICE_OFFSET) {
  727. struct parisc_device *dev;
  728. /* Was the device already added by Firmware? */
  729. dev = find_device_by_addr(hpa);
  730. if (!dev) {
  731. path.mod = i;
  732. dev = alloc_pa_dev(hpa, &path);
  733. if (!dev)
  734. continue;
  735. register_parisc_device(dev);
  736. devices_found++;
  737. }
  738. walk_lower_bus(dev);
  739. }
  740. } while(!devices_found && hpa < io_io_high);
  741. }
  742. #define CENTRAL_BUS_ADDR F_EXTEND(0xfff80000)
  743. /**
  744. * walk_central_bus - Find devices attached to the central bus
  745. *
  746. * PDC doesn't tell us about all devices in the system. This routine
  747. * finds devices connected to the central bus.
  748. */
  749. void __init walk_central_bus(void)
  750. {
  751. walk_native_bus(CENTRAL_BUS_ADDR,
  752. CENTRAL_BUS_ADDR + (MAX_NATIVE_DEVICES * NATIVE_DEVICE_OFFSET),
  753. &root);
  754. }
  755. static __init void print_parisc_device(struct parisc_device *dev)
  756. {
  757. static int count __initdata;
  758. pr_info("%d. %s at %pap { type:%d, hv:%#x, sv:%#x, rev:%#x }",
  759. ++count, dev->name, &(dev->hpa.start), dev->id.hw_type,
  760. dev->id.hversion, dev->id.sversion, dev->id.hversion_rev);
  761. if (dev->num_addrs) {
  762. int k;
  763. pr_cont(", additional addresses: ");
  764. for (k = 0; k < dev->num_addrs; k++)
  765. pr_cont("0x%lx ", dev->addr[k]);
  766. }
  767. pr_cont("\n");
  768. }
  769. /**
  770. * init_parisc_bus - Some preparation to be done before inventory
  771. */
  772. void __init init_parisc_bus(void)
  773. {
  774. if (bus_register(&parisc_bus_type))
  775. panic("Could not register PA-RISC bus type\n");
  776. if (device_register(&root))
  777. panic("Could not register PA-RISC root device\n");
  778. get_device(&root);
  779. }
  780. static __init void qemu_header(void)
  781. {
  782. int num;
  783. unsigned long *p;
  784. pr_info("--- cut here ---\n");
  785. pr_info("/* AUTO-GENERATED HEADER FILE FOR SEABIOS FIRMWARE */\n");
  786. pr_cont("/* generated with Linux kernel */\n");
  787. pr_cont("/* search for PARISC_QEMU_MACHINE_HEADER in Linux */\n\n");
  788. pr_info("#define PARISC_MODEL \"%s\"\n\n",
  789. boot_cpu_data.pdc.sys_model_name);
  790. #define p ((unsigned long *)&boot_cpu_data.pdc.model)
  791. pr_info("#define PARISC_PDC_MODEL 0x%lx, 0x%lx, 0x%lx, "
  792. "0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx\n\n",
  793. p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8]);
  794. #undef p
  795. pr_info("#define PARISC_PDC_VERSION 0x%04lx\n\n",
  796. boot_cpu_data.pdc.versions);
  797. pr_info("#define PARISC_PDC_CPUID 0x%04lx\n\n",
  798. boot_cpu_data.pdc.cpuid);
  799. pr_info("#define PARISC_PDC_CAPABILITIES 0x%04lx\n\n",
  800. boot_cpu_data.pdc.capabilities);
  801. pr_info("#define PARISC_PDC_ENTRY_ORG 0x%04lx\n\n",
  802. #ifdef CONFIG_64BIT
  803. (unsigned long)(PAGE0->mem_pdc_hi) << 32 |
  804. #endif
  805. (unsigned long)PAGE0->mem_pdc);
  806. pr_info("#define PARISC_PDC_CACHE_INFO");
  807. p = (unsigned long *) &cache_info;
  808. for (num = 0; num < sizeof(cache_info); num += sizeof(unsigned long)) {
  809. if (((num % 5) == 0)) {
  810. pr_cont(" \\\n");
  811. pr_info("\t");
  812. }
  813. pr_cont("%s0x%04lx",
  814. num?", ":"", *p++);
  815. }
  816. pr_cont("\n\n");
  817. }
  818. static __init int qemu_print_hpa(struct device *lin_dev, void *data)
  819. {
  820. struct parisc_device *dev = to_parisc_device(lin_dev);
  821. unsigned long hpa = dev->hpa.start;
  822. pr_cont("\t{\t.hpa = 0x%08lx,\\\n", hpa);
  823. pr_cont("\t\t.iodc = &iodc_data_hpa_%08lx,\\\n", hpa);
  824. pr_cont("\t\t.mod_info = &mod_info_hpa_%08lx,\\\n", hpa);
  825. pr_cont("\t\t.mod_path = &mod_path_hpa_%08lx,\\\n", hpa);
  826. pr_cont("\t\t.num_addr = HPA_%08lx_num_addr,\\\n", hpa);
  827. pr_cont("\t\t.add_addr = { HPA_%08lx_add_addr } },\\\n", hpa);
  828. return 0;
  829. }
  830. static __init void qemu_footer(void)
  831. {
  832. pr_info("\n\n#define PARISC_DEVICE_LIST \\\n");
  833. for_each_padev(qemu_print_hpa, NULL);
  834. pr_cont("\t{ 0, }\n");
  835. pr_info("--- cut here ---\n");
  836. }
  837. /* print iodc data of the various hpa modules for qemu inclusion */
  838. static __init int qemu_print_iodc_data(struct device *lin_dev, void *data)
  839. {
  840. struct parisc_device *dev = to_parisc_device(lin_dev);
  841. unsigned long count;
  842. unsigned long hpa = dev->hpa.start;
  843. int status;
  844. struct pdc_iodc iodc_data;
  845. int mod_index;
  846. struct pdc_system_map_mod_info pdc_mod_info;
  847. struct pdc_module_path mod_path;
  848. status = pdc_iodc_read(&count, hpa, 0,
  849. &iodc_data, sizeof(iodc_data));
  850. if (status != PDC_OK) {
  851. pr_info("No IODC data for hpa 0x%08lx\n", hpa);
  852. return 0;
  853. }
  854. pr_info("\n");
  855. pr_info("#define HPA_%08lx_DESCRIPTION \"%s\"\n",
  856. hpa, parisc_hardware_description(&dev->id));
  857. mod_index = 0;
  858. do {
  859. status = pdc_system_map_find_mods(&pdc_mod_info,
  860. &mod_path, mod_index++);
  861. } while (status == PDC_OK && pdc_mod_info.mod_addr != hpa);
  862. pr_info("static struct pdc_system_map_mod_info"
  863. " mod_info_hpa_%08lx = {\n", hpa);
  864. #define DO(member) \
  865. pr_cont("\t." #member " = 0x%x,\n", \
  866. (unsigned int)pdc_mod_info.member)
  867. DO(mod_addr);
  868. DO(mod_pgs);
  869. DO(add_addrs);
  870. pr_cont("};\n");
  871. #undef DO
  872. pr_info("static struct pdc_module_path "
  873. "mod_path_hpa_%08lx = {\n", hpa);
  874. pr_cont("\t.path = { ");
  875. pr_cont(".flags = 0x%x, ", mod_path.path.flags);
  876. pr_cont(".bc = { 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x }, ",
  877. (unsigned char)mod_path.path.bc[0],
  878. (unsigned char)mod_path.path.bc[1],
  879. (unsigned char)mod_path.path.bc[2],
  880. (unsigned char)mod_path.path.bc[3],
  881. (unsigned char)mod_path.path.bc[4],
  882. (unsigned char)mod_path.path.bc[5]);
  883. pr_cont(".mod = 0x%x ", mod_path.path.mod);
  884. pr_cont(" },\n");
  885. pr_cont("\t.layers = { 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x }\n",
  886. mod_path.layers[0], mod_path.layers[1], mod_path.layers[2],
  887. mod_path.layers[3], mod_path.layers[4], mod_path.layers[5]);
  888. pr_cont("};\n");
  889. pr_info("static struct pdc_iodc iodc_data_hpa_%08lx = {\n", hpa);
  890. #define DO(member) \
  891. pr_cont("\t." #member " = 0x%04lx,\n", \
  892. (unsigned long)iodc_data.member)
  893. DO(hversion_model);
  894. DO(hversion);
  895. DO(spa);
  896. DO(type);
  897. DO(sversion_rev);
  898. DO(sversion_model);
  899. DO(sversion_opt);
  900. DO(rev);
  901. DO(dep);
  902. DO(features);
  903. DO(checksum);
  904. DO(length);
  905. #undef DO
  906. pr_cont("\t/* pad: 0x%04x, 0x%04x */\n",
  907. iodc_data.pad[0], iodc_data.pad[1]);
  908. pr_cont("};\n");
  909. pr_info("#define HPA_%08lx_num_addr %d\n", hpa, dev->num_addrs);
  910. pr_info("#define HPA_%08lx_add_addr ", hpa);
  911. count = 0;
  912. if (dev->num_addrs == 0)
  913. pr_cont("0");
  914. while (count < dev->num_addrs) {
  915. pr_cont("0x%08lx, ", dev->addr[count]);
  916. count++;
  917. }
  918. pr_cont("\n\n");
  919. return 0;
  920. }
  921. static __init int print_one_device(struct device * dev, void * data)
  922. {
  923. struct parisc_device * pdev = to_parisc_device(dev);
  924. if (check_dev(dev))
  925. print_parisc_device(pdev);
  926. return 0;
  927. }
  928. /**
  929. * print_parisc_devices - Print out a list of devices found in this system
  930. */
  931. void __init print_parisc_devices(void)
  932. {
  933. for_each_padev(print_one_device, NULL);
  934. #define PARISC_QEMU_MACHINE_HEADER 0
  935. if (PARISC_QEMU_MACHINE_HEADER) {
  936. qemu_header();
  937. for_each_padev(qemu_print_iodc_data, NULL);
  938. qemu_footer();
  939. }
  940. }