conf_space_header.c 9.4 KB

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