conf_space_header.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. * PCI Backend - Handles the virtual fields in the configuration space headers.
  3. *
  4. * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/pci.h>
  8. #include "pciback.h"
  9. #include "conf_space.h"
  10. struct pci_bar_info {
  11. u32 val;
  12. u32 len_val;
  13. int which;
  14. };
  15. #define is_enable_cmd(value) ((value)&(PCI_COMMAND_MEMORY|PCI_COMMAND_IO))
  16. #define is_master_cmd(value) ((value)&PCI_COMMAND_MASTER)
  17. static int command_read(struct pci_dev *dev, int offset, u16 *value, void *data)
  18. {
  19. int i;
  20. int ret;
  21. ret = xen_pcibk_read_config_word(dev, offset, value, data);
  22. if (!pci_is_enabled(dev))
  23. return ret;
  24. for (i = 0; i < PCI_ROM_RESOURCE; i++) {
  25. if (dev->resource[i].flags & IORESOURCE_IO)
  26. *value |= PCI_COMMAND_IO;
  27. if (dev->resource[i].flags & IORESOURCE_MEM)
  28. *value |= PCI_COMMAND_MEMORY;
  29. }
  30. return ret;
  31. }
  32. static int command_write(struct pci_dev *dev, int offset, u16 value, void *data)
  33. {
  34. struct xen_pcibk_dev_data *dev_data;
  35. int err;
  36. dev_data = pci_get_drvdata(dev);
  37. if (!pci_is_enabled(dev) && is_enable_cmd(value)) {
  38. if (unlikely(verbose_request))
  39. printk(KERN_DEBUG DRV_NAME ": %s: enable\n",
  40. pci_name(dev));
  41. err = pci_enable_device(dev);
  42. if (err)
  43. return err;
  44. if (dev_data)
  45. dev_data->enable_intx = 1;
  46. } else if (pci_is_enabled(dev) && !is_enable_cmd(value)) {
  47. if (unlikely(verbose_request))
  48. printk(KERN_DEBUG DRV_NAME ": %s: disable\n",
  49. pci_name(dev));
  50. pci_disable_device(dev);
  51. if (dev_data)
  52. dev_data->enable_intx = 0;
  53. }
  54. if (!dev->is_busmaster && is_master_cmd(value)) {
  55. if (unlikely(verbose_request))
  56. printk(KERN_DEBUG DRV_NAME ": %s: set bus master\n",
  57. pci_name(dev));
  58. pci_set_master(dev);
  59. }
  60. if (value & PCI_COMMAND_INVALIDATE) {
  61. if (unlikely(verbose_request))
  62. printk(KERN_DEBUG
  63. DRV_NAME ": %s: enable memory-write-invalidate\n",
  64. pci_name(dev));
  65. err = pci_set_mwi(dev);
  66. if (err) {
  67. printk(KERN_WARNING
  68. DRV_NAME ": %s: cannot enable "
  69. "memory-write-invalidate (%d)\n",
  70. pci_name(dev), err);
  71. value &= ~PCI_COMMAND_INVALIDATE;
  72. }
  73. }
  74. return pci_write_config_word(dev, offset, value);
  75. }
  76. static int rom_write(struct pci_dev *dev, int offset, u32 value, void *data)
  77. {
  78. struct pci_bar_info *bar = data;
  79. if (unlikely(!bar)) {
  80. printk(KERN_WARNING DRV_NAME ": driver data not found for %s\n",
  81. pci_name(dev));
  82. return XEN_PCI_ERR_op_failed;
  83. }
  84. /* A write to obtain the length must happen as a 32-bit write.
  85. * This does not (yet) support writing individual bytes
  86. */
  87. if (value == ~PCI_ROM_ADDRESS_ENABLE)
  88. bar->which = 1;
  89. else {
  90. u32 tmpval;
  91. pci_read_config_dword(dev, offset, &tmpval);
  92. if (tmpval != bar->val && value == bar->val) {
  93. /* Allow restoration of bar value. */
  94. pci_write_config_dword(dev, offset, bar->val);
  95. }
  96. bar->which = 0;
  97. }
  98. /* Do we need to support enabling/disabling the rom address here? */
  99. return 0;
  100. }
  101. /* For the BARs, only allow writes which write ~0 or
  102. * the correct resource information
  103. * (Needed for when the driver probes the resource usage)
  104. */
  105. static int bar_write(struct pci_dev *dev, int offset, u32 value, void *data)
  106. {
  107. struct pci_bar_info *bar = data;
  108. if (unlikely(!bar)) {
  109. printk(KERN_WARNING DRV_NAME ": driver data not found for %s\n",
  110. pci_name(dev));
  111. return XEN_PCI_ERR_op_failed;
  112. }
  113. /* A write to obtain the length must happen as a 32-bit write.
  114. * This does not (yet) support writing individual bytes
  115. */
  116. if (value == ~0)
  117. bar->which = 1;
  118. else {
  119. u32 tmpval;
  120. pci_read_config_dword(dev, offset, &tmpval);
  121. if (tmpval != bar->val && value == bar->val) {
  122. /* Allow restoration of bar value. */
  123. pci_write_config_dword(dev, offset, bar->val);
  124. }
  125. bar->which = 0;
  126. }
  127. return 0;
  128. }
  129. static int bar_read(struct pci_dev *dev, int offset, u32 * value, void *data)
  130. {
  131. struct pci_bar_info *bar = data;
  132. if (unlikely(!bar)) {
  133. printk(KERN_WARNING DRV_NAME ": driver data not found for %s\n",
  134. pci_name(dev));
  135. return XEN_PCI_ERR_op_failed;
  136. }
  137. *value = bar->which ? bar->len_val : bar->val;
  138. return 0;
  139. }
  140. static inline void read_dev_bar(struct pci_dev *dev,
  141. struct pci_bar_info *bar_info, int offset,
  142. u32 len_mask)
  143. {
  144. int pos;
  145. struct resource *res = dev->resource;
  146. if (offset == PCI_ROM_ADDRESS || offset == PCI_ROM_ADDRESS1)
  147. pos = PCI_ROM_RESOURCE;
  148. else {
  149. pos = (offset - PCI_BASE_ADDRESS_0) / 4;
  150. if (pos && ((res[pos - 1].flags & (PCI_BASE_ADDRESS_SPACE |
  151. PCI_BASE_ADDRESS_MEM_TYPE_MASK)) ==
  152. (PCI_BASE_ADDRESS_SPACE_MEMORY |
  153. PCI_BASE_ADDRESS_MEM_TYPE_64))) {
  154. bar_info->val = res[pos - 1].start >> 32;
  155. bar_info->len_val = res[pos - 1].end >> 32;
  156. return;
  157. }
  158. }
  159. bar_info->val = res[pos].start |
  160. (res[pos].flags & PCI_REGION_FLAG_MASK);
  161. bar_info->len_val = resource_size(&res[pos]);
  162. }
  163. static void *bar_init(struct pci_dev *dev, int offset)
  164. {
  165. struct pci_bar_info *bar = kmalloc(sizeof(*bar), GFP_KERNEL);
  166. if (!bar)
  167. return ERR_PTR(-ENOMEM);
  168. read_dev_bar(dev, bar, offset, ~0);
  169. bar->which = 0;
  170. return bar;
  171. }
  172. static void *rom_init(struct pci_dev *dev, int offset)
  173. {
  174. struct pci_bar_info *bar = kmalloc(sizeof(*bar), GFP_KERNEL);
  175. if (!bar)
  176. return ERR_PTR(-ENOMEM);
  177. read_dev_bar(dev, bar, offset, ~PCI_ROM_ADDRESS_ENABLE);
  178. bar->which = 0;
  179. return bar;
  180. }
  181. static void bar_reset(struct pci_dev *dev, int offset, void *data)
  182. {
  183. struct pci_bar_info *bar = data;
  184. bar->which = 0;
  185. }
  186. static void bar_release(struct pci_dev *dev, int offset, void *data)
  187. {
  188. kfree(data);
  189. }
  190. static int xen_pcibk_read_vendor(struct pci_dev *dev, int offset,
  191. u16 *value, void *data)
  192. {
  193. *value = dev->vendor;
  194. return 0;
  195. }
  196. static int xen_pcibk_read_device(struct pci_dev *dev, int offset,
  197. u16 *value, void *data)
  198. {
  199. *value = dev->device;
  200. return 0;
  201. }
  202. static int interrupt_read(struct pci_dev *dev, int offset, u8 * value,
  203. void *data)
  204. {
  205. *value = (u8) dev->irq;
  206. return 0;
  207. }
  208. static int bist_write(struct pci_dev *dev, int offset, u8 value, void *data)
  209. {
  210. u8 cur_value;
  211. int err;
  212. err = pci_read_config_byte(dev, offset, &cur_value);
  213. if (err)
  214. goto out;
  215. if ((cur_value & ~PCI_BIST_START) == (value & ~PCI_BIST_START)
  216. || value == PCI_BIST_START)
  217. err = pci_write_config_byte(dev, offset, value);
  218. out:
  219. return err;
  220. }
  221. static const struct config_field header_common[] = {
  222. {
  223. .offset = PCI_VENDOR_ID,
  224. .size = 2,
  225. .u.w.read = xen_pcibk_read_vendor,
  226. },
  227. {
  228. .offset = PCI_DEVICE_ID,
  229. .size = 2,
  230. .u.w.read = xen_pcibk_read_device,
  231. },
  232. {
  233. .offset = PCI_COMMAND,
  234. .size = 2,
  235. .u.w.read = command_read,
  236. .u.w.write = command_write,
  237. },
  238. {
  239. .offset = PCI_INTERRUPT_LINE,
  240. .size = 1,
  241. .u.b.read = interrupt_read,
  242. },
  243. {
  244. .offset = PCI_INTERRUPT_PIN,
  245. .size = 1,
  246. .u.b.read = xen_pcibk_read_config_byte,
  247. },
  248. {
  249. /* Any side effects of letting driver domain control cache line? */
  250. .offset = PCI_CACHE_LINE_SIZE,
  251. .size = 1,
  252. .u.b.read = xen_pcibk_read_config_byte,
  253. .u.b.write = xen_pcibk_write_config_byte,
  254. },
  255. {
  256. .offset = PCI_LATENCY_TIMER,
  257. .size = 1,
  258. .u.b.read = xen_pcibk_read_config_byte,
  259. },
  260. {
  261. .offset = PCI_BIST,
  262. .size = 1,
  263. .u.b.read = xen_pcibk_read_config_byte,
  264. .u.b.write = bist_write,
  265. },
  266. {}
  267. };
  268. #define CFG_FIELD_BAR(reg_offset) \
  269. { \
  270. .offset = reg_offset, \
  271. .size = 4, \
  272. .init = bar_init, \
  273. .reset = bar_reset, \
  274. .release = bar_release, \
  275. .u.dw.read = bar_read, \
  276. .u.dw.write = bar_write, \
  277. }
  278. #define CFG_FIELD_ROM(reg_offset) \
  279. { \
  280. .offset = reg_offset, \
  281. .size = 4, \
  282. .init = rom_init, \
  283. .reset = bar_reset, \
  284. .release = bar_release, \
  285. .u.dw.read = bar_read, \
  286. .u.dw.write = rom_write, \
  287. }
  288. static const struct config_field header_0[] = {
  289. CFG_FIELD_BAR(PCI_BASE_ADDRESS_0),
  290. CFG_FIELD_BAR(PCI_BASE_ADDRESS_1),
  291. CFG_FIELD_BAR(PCI_BASE_ADDRESS_2),
  292. CFG_FIELD_BAR(PCI_BASE_ADDRESS_3),
  293. CFG_FIELD_BAR(PCI_BASE_ADDRESS_4),
  294. CFG_FIELD_BAR(PCI_BASE_ADDRESS_5),
  295. CFG_FIELD_ROM(PCI_ROM_ADDRESS),
  296. {}
  297. };
  298. static const struct config_field header_1[] = {
  299. CFG_FIELD_BAR(PCI_BASE_ADDRESS_0),
  300. CFG_FIELD_BAR(PCI_BASE_ADDRESS_1),
  301. CFG_FIELD_ROM(PCI_ROM_ADDRESS1),
  302. {}
  303. };
  304. int xen_pcibk_config_header_add_fields(struct pci_dev *dev)
  305. {
  306. int err;
  307. err = xen_pcibk_config_add_fields(dev, header_common);
  308. if (err)
  309. goto out;
  310. switch (dev->hdr_type) {
  311. case PCI_HEADER_TYPE_NORMAL:
  312. err = xen_pcibk_config_add_fields(dev, header_0);
  313. break;
  314. case PCI_HEADER_TYPE_BRIDGE:
  315. err = xen_pcibk_config_add_fields(dev, header_1);
  316. break;
  317. default:
  318. err = -EINVAL;
  319. printk(KERN_ERR DRV_NAME ": %s: Unsupported header type %d!\n",
  320. pci_name(dev), dev->hdr_type);
  321. break;
  322. }
  323. out:
  324. return err;
  325. }