vmbus_drv.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624
  1. /*
  2. * Copyright (c) 2009, Microsoft Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  15. * Place - Suite 330, Boston, MA 02111-1307 USA.
  16. *
  17. * Authors:
  18. * Haiyang Zhang <haiyangz@microsoft.com>
  19. * Hank Janssen <hjanssen@microsoft.com>
  20. * K. Y. Srinivasan <kys@microsoft.com>
  21. *
  22. */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #include <linux/init.h>
  25. #include <linux/module.h>
  26. #include <linux/device.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/sysctl.h>
  29. #include <linux/slab.h>
  30. #include <linux/acpi.h>
  31. #include <linux/completion.h>
  32. #include <linux/hyperv.h>
  33. #include <linux/kernel_stat.h>
  34. #include <linux/clockchips.h>
  35. #include <linux/cpu.h>
  36. #include <linux/sched/task_stack.h>
  37. #include <asm/hyperv.h>
  38. #include <asm/hypervisor.h>
  39. #include <asm/mshyperv.h>
  40. #include <linux/notifier.h>
  41. #include <linux/ptrace.h>
  42. #include <linux/screen_info.h>
  43. #include <linux/kdebug.h>
  44. #include <linux/efi.h>
  45. #include <linux/random.h>
  46. #include "hyperv_vmbus.h"
  47. struct vmbus_dynid {
  48. struct list_head node;
  49. struct hv_vmbus_device_id id;
  50. };
  51. static struct acpi_device *hv_acpi_dev;
  52. static struct completion probe_event;
  53. static int hyperv_cpuhp_online;
  54. static int hyperv_panic_event(struct notifier_block *nb, unsigned long val,
  55. void *args)
  56. {
  57. struct pt_regs *regs;
  58. regs = current_pt_regs();
  59. hyperv_report_panic(regs);
  60. return NOTIFY_DONE;
  61. }
  62. static int hyperv_die_event(struct notifier_block *nb, unsigned long val,
  63. void *args)
  64. {
  65. struct die_args *die = (struct die_args *)args;
  66. struct pt_regs *regs = die->regs;
  67. hyperv_report_panic(regs);
  68. return NOTIFY_DONE;
  69. }
  70. static struct notifier_block hyperv_die_block = {
  71. .notifier_call = hyperv_die_event,
  72. };
  73. static struct notifier_block hyperv_panic_block = {
  74. .notifier_call = hyperv_panic_event,
  75. };
  76. static const char *fb_mmio_name = "fb_range";
  77. static struct resource *fb_mmio;
  78. static struct resource *hyperv_mmio;
  79. static DEFINE_SEMAPHORE(hyperv_mmio_lock);
  80. static int vmbus_exists(void)
  81. {
  82. if (hv_acpi_dev == NULL)
  83. return -ENODEV;
  84. return 0;
  85. }
  86. #define VMBUS_ALIAS_LEN ((sizeof((struct hv_vmbus_device_id *)0)->guid) * 2)
  87. static void print_alias_name(struct hv_device *hv_dev, char *alias_name)
  88. {
  89. int i;
  90. for (i = 0; i < VMBUS_ALIAS_LEN; i += 2)
  91. sprintf(&alias_name[i], "%02x", hv_dev->dev_type.b[i/2]);
  92. }
  93. static u8 channel_monitor_group(struct vmbus_channel *channel)
  94. {
  95. return (u8)channel->offermsg.monitorid / 32;
  96. }
  97. static u8 channel_monitor_offset(struct vmbus_channel *channel)
  98. {
  99. return (u8)channel->offermsg.monitorid % 32;
  100. }
  101. static u32 channel_pending(struct vmbus_channel *channel,
  102. struct hv_monitor_page *monitor_page)
  103. {
  104. u8 monitor_group = channel_monitor_group(channel);
  105. return monitor_page->trigger_group[monitor_group].pending;
  106. }
  107. static u32 channel_latency(struct vmbus_channel *channel,
  108. struct hv_monitor_page *monitor_page)
  109. {
  110. u8 monitor_group = channel_monitor_group(channel);
  111. u8 monitor_offset = channel_monitor_offset(channel);
  112. return monitor_page->latency[monitor_group][monitor_offset];
  113. }
  114. static u32 channel_conn_id(struct vmbus_channel *channel,
  115. struct hv_monitor_page *monitor_page)
  116. {
  117. u8 monitor_group = channel_monitor_group(channel);
  118. u8 monitor_offset = channel_monitor_offset(channel);
  119. return monitor_page->parameter[monitor_group][monitor_offset].connectionid.u.id;
  120. }
  121. static ssize_t id_show(struct device *dev, struct device_attribute *dev_attr,
  122. char *buf)
  123. {
  124. struct hv_device *hv_dev = device_to_hv_device(dev);
  125. if (!hv_dev->channel)
  126. return -ENODEV;
  127. return sprintf(buf, "%d\n", hv_dev->channel->offermsg.child_relid);
  128. }
  129. static DEVICE_ATTR_RO(id);
  130. static ssize_t state_show(struct device *dev, struct device_attribute *dev_attr,
  131. char *buf)
  132. {
  133. struct hv_device *hv_dev = device_to_hv_device(dev);
  134. if (!hv_dev->channel)
  135. return -ENODEV;
  136. return sprintf(buf, "%d\n", hv_dev->channel->state);
  137. }
  138. static DEVICE_ATTR_RO(state);
  139. static ssize_t monitor_id_show(struct device *dev,
  140. struct device_attribute *dev_attr, char *buf)
  141. {
  142. struct hv_device *hv_dev = device_to_hv_device(dev);
  143. if (!hv_dev->channel)
  144. return -ENODEV;
  145. return sprintf(buf, "%d\n", hv_dev->channel->offermsg.monitorid);
  146. }
  147. static DEVICE_ATTR_RO(monitor_id);
  148. static ssize_t class_id_show(struct device *dev,
  149. struct device_attribute *dev_attr, char *buf)
  150. {
  151. struct hv_device *hv_dev = device_to_hv_device(dev);
  152. if (!hv_dev->channel)
  153. return -ENODEV;
  154. return sprintf(buf, "{%pUl}\n",
  155. hv_dev->channel->offermsg.offer.if_type.b);
  156. }
  157. static DEVICE_ATTR_RO(class_id);
  158. static ssize_t device_id_show(struct device *dev,
  159. struct device_attribute *dev_attr, char *buf)
  160. {
  161. struct hv_device *hv_dev = device_to_hv_device(dev);
  162. if (!hv_dev->channel)
  163. return -ENODEV;
  164. return sprintf(buf, "{%pUl}\n",
  165. hv_dev->channel->offermsg.offer.if_instance.b);
  166. }
  167. static DEVICE_ATTR_RO(device_id);
  168. static ssize_t modalias_show(struct device *dev,
  169. struct device_attribute *dev_attr, char *buf)
  170. {
  171. struct hv_device *hv_dev = device_to_hv_device(dev);
  172. char alias_name[VMBUS_ALIAS_LEN + 1];
  173. print_alias_name(hv_dev, alias_name);
  174. return sprintf(buf, "vmbus:%s\n", alias_name);
  175. }
  176. static DEVICE_ATTR_RO(modalias);
  177. static ssize_t server_monitor_pending_show(struct device *dev,
  178. struct device_attribute *dev_attr,
  179. char *buf)
  180. {
  181. struct hv_device *hv_dev = device_to_hv_device(dev);
  182. if (!hv_dev->channel)
  183. return -ENODEV;
  184. return sprintf(buf, "%d\n",
  185. channel_pending(hv_dev->channel,
  186. vmbus_connection.monitor_pages[1]));
  187. }
  188. static DEVICE_ATTR_RO(server_monitor_pending);
  189. static ssize_t client_monitor_pending_show(struct device *dev,
  190. struct device_attribute *dev_attr,
  191. char *buf)
  192. {
  193. struct hv_device *hv_dev = device_to_hv_device(dev);
  194. if (!hv_dev->channel)
  195. return -ENODEV;
  196. return sprintf(buf, "%d\n",
  197. channel_pending(hv_dev->channel,
  198. vmbus_connection.monitor_pages[1]));
  199. }
  200. static DEVICE_ATTR_RO(client_monitor_pending);
  201. static ssize_t server_monitor_latency_show(struct device *dev,
  202. struct device_attribute *dev_attr,
  203. char *buf)
  204. {
  205. struct hv_device *hv_dev = device_to_hv_device(dev);
  206. if (!hv_dev->channel)
  207. return -ENODEV;
  208. return sprintf(buf, "%d\n",
  209. channel_latency(hv_dev->channel,
  210. vmbus_connection.monitor_pages[0]));
  211. }
  212. static DEVICE_ATTR_RO(server_monitor_latency);
  213. static ssize_t client_monitor_latency_show(struct device *dev,
  214. struct device_attribute *dev_attr,
  215. char *buf)
  216. {
  217. struct hv_device *hv_dev = device_to_hv_device(dev);
  218. if (!hv_dev->channel)
  219. return -ENODEV;
  220. return sprintf(buf, "%d\n",
  221. channel_latency(hv_dev->channel,
  222. vmbus_connection.monitor_pages[1]));
  223. }
  224. static DEVICE_ATTR_RO(client_monitor_latency);
  225. static ssize_t server_monitor_conn_id_show(struct device *dev,
  226. struct device_attribute *dev_attr,
  227. char *buf)
  228. {
  229. struct hv_device *hv_dev = device_to_hv_device(dev);
  230. if (!hv_dev->channel)
  231. return -ENODEV;
  232. return sprintf(buf, "%d\n",
  233. channel_conn_id(hv_dev->channel,
  234. vmbus_connection.monitor_pages[0]));
  235. }
  236. static DEVICE_ATTR_RO(server_monitor_conn_id);
  237. static ssize_t client_monitor_conn_id_show(struct device *dev,
  238. struct device_attribute *dev_attr,
  239. char *buf)
  240. {
  241. struct hv_device *hv_dev = device_to_hv_device(dev);
  242. if (!hv_dev->channel)
  243. return -ENODEV;
  244. return sprintf(buf, "%d\n",
  245. channel_conn_id(hv_dev->channel,
  246. vmbus_connection.monitor_pages[1]));
  247. }
  248. static DEVICE_ATTR_RO(client_monitor_conn_id);
  249. static ssize_t out_intr_mask_show(struct device *dev,
  250. struct device_attribute *dev_attr, char *buf)
  251. {
  252. struct hv_device *hv_dev = device_to_hv_device(dev);
  253. struct hv_ring_buffer_debug_info outbound;
  254. if (!hv_dev->channel)
  255. return -ENODEV;
  256. hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
  257. return sprintf(buf, "%d\n", outbound.current_interrupt_mask);
  258. }
  259. static DEVICE_ATTR_RO(out_intr_mask);
  260. static ssize_t out_read_index_show(struct device *dev,
  261. struct device_attribute *dev_attr, char *buf)
  262. {
  263. struct hv_device *hv_dev = device_to_hv_device(dev);
  264. struct hv_ring_buffer_debug_info outbound;
  265. if (!hv_dev->channel)
  266. return -ENODEV;
  267. hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
  268. return sprintf(buf, "%d\n", outbound.current_read_index);
  269. }
  270. static DEVICE_ATTR_RO(out_read_index);
  271. static ssize_t out_write_index_show(struct device *dev,
  272. struct device_attribute *dev_attr,
  273. char *buf)
  274. {
  275. struct hv_device *hv_dev = device_to_hv_device(dev);
  276. struct hv_ring_buffer_debug_info outbound;
  277. if (!hv_dev->channel)
  278. return -ENODEV;
  279. hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
  280. return sprintf(buf, "%d\n", outbound.current_write_index);
  281. }
  282. static DEVICE_ATTR_RO(out_write_index);
  283. static ssize_t out_read_bytes_avail_show(struct device *dev,
  284. struct device_attribute *dev_attr,
  285. char *buf)
  286. {
  287. struct hv_device *hv_dev = device_to_hv_device(dev);
  288. struct hv_ring_buffer_debug_info outbound;
  289. if (!hv_dev->channel)
  290. return -ENODEV;
  291. hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
  292. return sprintf(buf, "%d\n", outbound.bytes_avail_toread);
  293. }
  294. static DEVICE_ATTR_RO(out_read_bytes_avail);
  295. static ssize_t out_write_bytes_avail_show(struct device *dev,
  296. struct device_attribute *dev_attr,
  297. char *buf)
  298. {
  299. struct hv_device *hv_dev = device_to_hv_device(dev);
  300. struct hv_ring_buffer_debug_info outbound;
  301. if (!hv_dev->channel)
  302. return -ENODEV;
  303. hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
  304. return sprintf(buf, "%d\n", outbound.bytes_avail_towrite);
  305. }
  306. static DEVICE_ATTR_RO(out_write_bytes_avail);
  307. static ssize_t in_intr_mask_show(struct device *dev,
  308. struct device_attribute *dev_attr, char *buf)
  309. {
  310. struct hv_device *hv_dev = device_to_hv_device(dev);
  311. struct hv_ring_buffer_debug_info inbound;
  312. if (!hv_dev->channel)
  313. return -ENODEV;
  314. hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  315. return sprintf(buf, "%d\n", inbound.current_interrupt_mask);
  316. }
  317. static DEVICE_ATTR_RO(in_intr_mask);
  318. static ssize_t in_read_index_show(struct device *dev,
  319. struct device_attribute *dev_attr, char *buf)
  320. {
  321. struct hv_device *hv_dev = device_to_hv_device(dev);
  322. struct hv_ring_buffer_debug_info inbound;
  323. if (!hv_dev->channel)
  324. return -ENODEV;
  325. hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  326. return sprintf(buf, "%d\n", inbound.current_read_index);
  327. }
  328. static DEVICE_ATTR_RO(in_read_index);
  329. static ssize_t in_write_index_show(struct device *dev,
  330. struct device_attribute *dev_attr, char *buf)
  331. {
  332. struct hv_device *hv_dev = device_to_hv_device(dev);
  333. struct hv_ring_buffer_debug_info inbound;
  334. if (!hv_dev->channel)
  335. return -ENODEV;
  336. hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  337. return sprintf(buf, "%d\n", inbound.current_write_index);
  338. }
  339. static DEVICE_ATTR_RO(in_write_index);
  340. static ssize_t in_read_bytes_avail_show(struct device *dev,
  341. struct device_attribute *dev_attr,
  342. char *buf)
  343. {
  344. struct hv_device *hv_dev = device_to_hv_device(dev);
  345. struct hv_ring_buffer_debug_info inbound;
  346. if (!hv_dev->channel)
  347. return -ENODEV;
  348. hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  349. return sprintf(buf, "%d\n", inbound.bytes_avail_toread);
  350. }
  351. static DEVICE_ATTR_RO(in_read_bytes_avail);
  352. static ssize_t in_write_bytes_avail_show(struct device *dev,
  353. struct device_attribute *dev_attr,
  354. char *buf)
  355. {
  356. struct hv_device *hv_dev = device_to_hv_device(dev);
  357. struct hv_ring_buffer_debug_info inbound;
  358. if (!hv_dev->channel)
  359. return -ENODEV;
  360. hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  361. return sprintf(buf, "%d\n", inbound.bytes_avail_towrite);
  362. }
  363. static DEVICE_ATTR_RO(in_write_bytes_avail);
  364. static ssize_t channel_vp_mapping_show(struct device *dev,
  365. struct device_attribute *dev_attr,
  366. char *buf)
  367. {
  368. struct hv_device *hv_dev = device_to_hv_device(dev);
  369. struct vmbus_channel *channel = hv_dev->channel, *cur_sc;
  370. unsigned long flags;
  371. int buf_size = PAGE_SIZE, n_written, tot_written;
  372. struct list_head *cur;
  373. if (!channel)
  374. return -ENODEV;
  375. tot_written = snprintf(buf, buf_size, "%u:%u\n",
  376. channel->offermsg.child_relid, channel->target_cpu);
  377. spin_lock_irqsave(&channel->lock, flags);
  378. list_for_each(cur, &channel->sc_list) {
  379. if (tot_written >= buf_size - 1)
  380. break;
  381. cur_sc = list_entry(cur, struct vmbus_channel, sc_list);
  382. n_written = scnprintf(buf + tot_written,
  383. buf_size - tot_written,
  384. "%u:%u\n",
  385. cur_sc->offermsg.child_relid,
  386. cur_sc->target_cpu);
  387. tot_written += n_written;
  388. }
  389. spin_unlock_irqrestore(&channel->lock, flags);
  390. return tot_written;
  391. }
  392. static DEVICE_ATTR_RO(channel_vp_mapping);
  393. static ssize_t vendor_show(struct device *dev,
  394. struct device_attribute *dev_attr,
  395. char *buf)
  396. {
  397. struct hv_device *hv_dev = device_to_hv_device(dev);
  398. return sprintf(buf, "0x%x\n", hv_dev->vendor_id);
  399. }
  400. static DEVICE_ATTR_RO(vendor);
  401. static ssize_t device_show(struct device *dev,
  402. struct device_attribute *dev_attr,
  403. char *buf)
  404. {
  405. struct hv_device *hv_dev = device_to_hv_device(dev);
  406. return sprintf(buf, "0x%x\n", hv_dev->device_id);
  407. }
  408. static DEVICE_ATTR_RO(device);
  409. /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
  410. static struct attribute *vmbus_dev_attrs[] = {
  411. &dev_attr_id.attr,
  412. &dev_attr_state.attr,
  413. &dev_attr_monitor_id.attr,
  414. &dev_attr_class_id.attr,
  415. &dev_attr_device_id.attr,
  416. &dev_attr_modalias.attr,
  417. &dev_attr_server_monitor_pending.attr,
  418. &dev_attr_client_monitor_pending.attr,
  419. &dev_attr_server_monitor_latency.attr,
  420. &dev_attr_client_monitor_latency.attr,
  421. &dev_attr_server_monitor_conn_id.attr,
  422. &dev_attr_client_monitor_conn_id.attr,
  423. &dev_attr_out_intr_mask.attr,
  424. &dev_attr_out_read_index.attr,
  425. &dev_attr_out_write_index.attr,
  426. &dev_attr_out_read_bytes_avail.attr,
  427. &dev_attr_out_write_bytes_avail.attr,
  428. &dev_attr_in_intr_mask.attr,
  429. &dev_attr_in_read_index.attr,
  430. &dev_attr_in_write_index.attr,
  431. &dev_attr_in_read_bytes_avail.attr,
  432. &dev_attr_in_write_bytes_avail.attr,
  433. &dev_attr_channel_vp_mapping.attr,
  434. &dev_attr_vendor.attr,
  435. &dev_attr_device.attr,
  436. NULL,
  437. };
  438. ATTRIBUTE_GROUPS(vmbus_dev);
  439. /*
  440. * vmbus_uevent - add uevent for our device
  441. *
  442. * This routine is invoked when a device is added or removed on the vmbus to
  443. * generate a uevent to udev in the userspace. The udev will then look at its
  444. * rule and the uevent generated here to load the appropriate driver
  445. *
  446. * The alias string will be of the form vmbus:guid where guid is the string
  447. * representation of the device guid (each byte of the guid will be
  448. * represented with two hex characters.
  449. */
  450. static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
  451. {
  452. struct hv_device *dev = device_to_hv_device(device);
  453. int ret;
  454. char alias_name[VMBUS_ALIAS_LEN + 1];
  455. print_alias_name(dev, alias_name);
  456. ret = add_uevent_var(env, "MODALIAS=vmbus:%s", alias_name);
  457. return ret;
  458. }
  459. static const uuid_le null_guid;
  460. static inline bool is_null_guid(const uuid_le *guid)
  461. {
  462. if (uuid_le_cmp(*guid, null_guid))
  463. return false;
  464. return true;
  465. }
  466. /*
  467. * Return a matching hv_vmbus_device_id pointer.
  468. * If there is no match, return NULL.
  469. */
  470. static const struct hv_vmbus_device_id *hv_vmbus_get_id(struct hv_driver *drv,
  471. const uuid_le *guid)
  472. {
  473. const struct hv_vmbus_device_id *id = NULL;
  474. struct vmbus_dynid *dynid;
  475. /* Look at the dynamic ids first, before the static ones */
  476. spin_lock(&drv->dynids.lock);
  477. list_for_each_entry(dynid, &drv->dynids.list, node) {
  478. if (!uuid_le_cmp(dynid->id.guid, *guid)) {
  479. id = &dynid->id;
  480. break;
  481. }
  482. }
  483. spin_unlock(&drv->dynids.lock);
  484. if (id)
  485. return id;
  486. id = drv->id_table;
  487. if (id == NULL)
  488. return NULL; /* empty device table */
  489. for (; !is_null_guid(&id->guid); id++)
  490. if (!uuid_le_cmp(id->guid, *guid))
  491. return id;
  492. return NULL;
  493. }
  494. /* vmbus_add_dynid - add a new device ID to this driver and re-probe devices */
  495. static int vmbus_add_dynid(struct hv_driver *drv, uuid_le *guid)
  496. {
  497. struct vmbus_dynid *dynid;
  498. dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
  499. if (!dynid)
  500. return -ENOMEM;
  501. dynid->id.guid = *guid;
  502. spin_lock(&drv->dynids.lock);
  503. list_add_tail(&dynid->node, &drv->dynids.list);
  504. spin_unlock(&drv->dynids.lock);
  505. return driver_attach(&drv->driver);
  506. }
  507. static void vmbus_free_dynids(struct hv_driver *drv)
  508. {
  509. struct vmbus_dynid *dynid, *n;
  510. spin_lock(&drv->dynids.lock);
  511. list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
  512. list_del(&dynid->node);
  513. kfree(dynid);
  514. }
  515. spin_unlock(&drv->dynids.lock);
  516. }
  517. /* Parse string of form: 1b4e28ba-2fa1-11d2-883f-b9a761bde3f */
  518. static int get_uuid_le(const char *str, uuid_le *uu)
  519. {
  520. unsigned int b[16];
  521. int i;
  522. if (strlen(str) < 37)
  523. return -1;
  524. for (i = 0; i < 36; i++) {
  525. switch (i) {
  526. case 8: case 13: case 18: case 23:
  527. if (str[i] != '-')
  528. return -1;
  529. break;
  530. default:
  531. if (!isxdigit(str[i]))
  532. return -1;
  533. }
  534. }
  535. /* unparse little endian output byte order */
  536. if (sscanf(str,
  537. "%2x%2x%2x%2x-%2x%2x-%2x%2x-%2x%2x-%2x%2x%2x%2x%2x%2x",
  538. &b[3], &b[2], &b[1], &b[0],
  539. &b[5], &b[4], &b[7], &b[6], &b[8], &b[9],
  540. &b[10], &b[11], &b[12], &b[13], &b[14], &b[15]) != 16)
  541. return -1;
  542. for (i = 0; i < 16; i++)
  543. uu->b[i] = b[i];
  544. return 0;
  545. }
  546. /*
  547. * store_new_id - sysfs frontend to vmbus_add_dynid()
  548. *
  549. * Allow GUIDs to be added to an existing driver via sysfs.
  550. */
  551. static ssize_t new_id_store(struct device_driver *driver, const char *buf,
  552. size_t count)
  553. {
  554. struct hv_driver *drv = drv_to_hv_drv(driver);
  555. uuid_le guid = NULL_UUID_LE;
  556. ssize_t retval;
  557. if (get_uuid_le(buf, &guid) != 0)
  558. return -EINVAL;
  559. if (hv_vmbus_get_id(drv, &guid))
  560. return -EEXIST;
  561. retval = vmbus_add_dynid(drv, &guid);
  562. if (retval)
  563. return retval;
  564. return count;
  565. }
  566. static DRIVER_ATTR_WO(new_id);
  567. /*
  568. * store_remove_id - remove a PCI device ID from this driver
  569. *
  570. * Removes a dynamic pci device ID to this driver.
  571. */
  572. static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
  573. size_t count)
  574. {
  575. struct hv_driver *drv = drv_to_hv_drv(driver);
  576. struct vmbus_dynid *dynid, *n;
  577. uuid_le guid = NULL_UUID_LE;
  578. size_t retval = -ENODEV;
  579. if (get_uuid_le(buf, &guid))
  580. return -EINVAL;
  581. spin_lock(&drv->dynids.lock);
  582. list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
  583. struct hv_vmbus_device_id *id = &dynid->id;
  584. if (!uuid_le_cmp(id->guid, guid)) {
  585. list_del(&dynid->node);
  586. kfree(dynid);
  587. retval = count;
  588. break;
  589. }
  590. }
  591. spin_unlock(&drv->dynids.lock);
  592. return retval;
  593. }
  594. static DRIVER_ATTR_WO(remove_id);
  595. static struct attribute *vmbus_drv_attrs[] = {
  596. &driver_attr_new_id.attr,
  597. &driver_attr_remove_id.attr,
  598. NULL,
  599. };
  600. ATTRIBUTE_GROUPS(vmbus_drv);
  601. /*
  602. * vmbus_match - Attempt to match the specified device to the specified driver
  603. */
  604. static int vmbus_match(struct device *device, struct device_driver *driver)
  605. {
  606. struct hv_driver *drv = drv_to_hv_drv(driver);
  607. struct hv_device *hv_dev = device_to_hv_device(device);
  608. /* The hv_sock driver handles all hv_sock offers. */
  609. if (is_hvsock_channel(hv_dev->channel))
  610. return drv->hvsock;
  611. if (hv_vmbus_get_id(drv, &hv_dev->dev_type))
  612. return 1;
  613. return 0;
  614. }
  615. /*
  616. * vmbus_probe - Add the new vmbus's child device
  617. */
  618. static int vmbus_probe(struct device *child_device)
  619. {
  620. int ret = 0;
  621. struct hv_driver *drv =
  622. drv_to_hv_drv(child_device->driver);
  623. struct hv_device *dev = device_to_hv_device(child_device);
  624. const struct hv_vmbus_device_id *dev_id;
  625. dev_id = hv_vmbus_get_id(drv, &dev->dev_type);
  626. if (drv->probe) {
  627. ret = drv->probe(dev, dev_id);
  628. if (ret != 0)
  629. pr_err("probe failed for device %s (%d)\n",
  630. dev_name(child_device), ret);
  631. } else {
  632. pr_err("probe not set for driver %s\n",
  633. dev_name(child_device));
  634. ret = -ENODEV;
  635. }
  636. return ret;
  637. }
  638. /*
  639. * vmbus_remove - Remove a vmbus device
  640. */
  641. static int vmbus_remove(struct device *child_device)
  642. {
  643. struct hv_driver *drv;
  644. struct hv_device *dev = device_to_hv_device(child_device);
  645. if (child_device->driver) {
  646. drv = drv_to_hv_drv(child_device->driver);
  647. if (drv->remove)
  648. drv->remove(dev);
  649. }
  650. return 0;
  651. }
  652. /*
  653. * vmbus_shutdown - Shutdown a vmbus device
  654. */
  655. static void vmbus_shutdown(struct device *child_device)
  656. {
  657. struct hv_driver *drv;
  658. struct hv_device *dev = device_to_hv_device(child_device);
  659. /* The device may not be attached yet */
  660. if (!child_device->driver)
  661. return;
  662. drv = drv_to_hv_drv(child_device->driver);
  663. if (drv->shutdown)
  664. drv->shutdown(dev);
  665. }
  666. /*
  667. * vmbus_device_release - Final callback release of the vmbus child device
  668. */
  669. static void vmbus_device_release(struct device *device)
  670. {
  671. struct hv_device *hv_dev = device_to_hv_device(device);
  672. struct vmbus_channel *channel = hv_dev->channel;
  673. hv_process_channel_removal(channel,
  674. channel->offermsg.child_relid);
  675. kfree(hv_dev);
  676. }
  677. /* The one and only one */
  678. static struct bus_type hv_bus = {
  679. .name = "vmbus",
  680. .match = vmbus_match,
  681. .shutdown = vmbus_shutdown,
  682. .remove = vmbus_remove,
  683. .probe = vmbus_probe,
  684. .uevent = vmbus_uevent,
  685. .dev_groups = vmbus_dev_groups,
  686. .drv_groups = vmbus_drv_groups,
  687. };
  688. struct onmessage_work_context {
  689. struct work_struct work;
  690. struct hv_message msg;
  691. };
  692. static void vmbus_onmessage_work(struct work_struct *work)
  693. {
  694. struct onmessage_work_context *ctx;
  695. /* Do not process messages if we're in DISCONNECTED state */
  696. if (vmbus_connection.conn_state == DISCONNECTED)
  697. return;
  698. ctx = container_of(work, struct onmessage_work_context,
  699. work);
  700. vmbus_onmessage(&ctx->msg);
  701. kfree(ctx);
  702. }
  703. static void hv_process_timer_expiration(struct hv_message *msg,
  704. struct hv_per_cpu_context *hv_cpu)
  705. {
  706. struct clock_event_device *dev = hv_cpu->clk_evt;
  707. if (dev->event_handler)
  708. dev->event_handler(dev);
  709. vmbus_signal_eom(msg, HVMSG_TIMER_EXPIRED);
  710. }
  711. void vmbus_on_msg_dpc(unsigned long data)
  712. {
  713. struct hv_per_cpu_context *hv_cpu = (void *)data;
  714. void *page_addr = hv_cpu->synic_message_page;
  715. struct hv_message *msg = (struct hv_message *)page_addr +
  716. VMBUS_MESSAGE_SINT;
  717. struct vmbus_channel_message_header *hdr;
  718. const struct vmbus_channel_message_table_entry *entry;
  719. struct onmessage_work_context *ctx;
  720. u32 message_type = msg->header.message_type;
  721. if (message_type == HVMSG_NONE)
  722. /* no msg */
  723. return;
  724. hdr = (struct vmbus_channel_message_header *)msg->u.payload;
  725. if (hdr->msgtype >= CHANNELMSG_COUNT) {
  726. WARN_ONCE(1, "unknown msgtype=%d\n", hdr->msgtype);
  727. goto msg_handled;
  728. }
  729. entry = &channel_message_table[hdr->msgtype];
  730. if (entry->handler_type == VMHT_BLOCKING) {
  731. ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC);
  732. if (ctx == NULL)
  733. return;
  734. INIT_WORK(&ctx->work, vmbus_onmessage_work);
  735. memcpy(&ctx->msg, msg, sizeof(*msg));
  736. queue_work(vmbus_connection.work_queue, &ctx->work);
  737. } else
  738. entry->message_handler(hdr);
  739. msg_handled:
  740. vmbus_signal_eom(msg, message_type);
  741. }
  742. /*
  743. * Direct callback for channels using other deferred processing
  744. */
  745. static void vmbus_channel_isr(struct vmbus_channel *channel)
  746. {
  747. void (*callback_fn)(void *);
  748. callback_fn = READ_ONCE(channel->onchannel_callback);
  749. if (likely(callback_fn != NULL))
  750. (*callback_fn)(channel->channel_callback_context);
  751. }
  752. /*
  753. * Schedule all channels with events pending
  754. */
  755. static void vmbus_chan_sched(struct hv_per_cpu_context *hv_cpu)
  756. {
  757. unsigned long *recv_int_page;
  758. u32 maxbits, relid;
  759. if (vmbus_proto_version < VERSION_WIN8) {
  760. maxbits = MAX_NUM_CHANNELS_SUPPORTED;
  761. recv_int_page = vmbus_connection.recv_int_page;
  762. } else {
  763. /*
  764. * When the host is win8 and beyond, the event page
  765. * can be directly checked to get the id of the channel
  766. * that has the interrupt pending.
  767. */
  768. void *page_addr = hv_cpu->synic_event_page;
  769. union hv_synic_event_flags *event
  770. = (union hv_synic_event_flags *)page_addr +
  771. VMBUS_MESSAGE_SINT;
  772. maxbits = HV_EVENT_FLAGS_COUNT;
  773. recv_int_page = event->flags;
  774. }
  775. if (unlikely(!recv_int_page))
  776. return;
  777. for_each_set_bit(relid, recv_int_page, maxbits) {
  778. struct vmbus_channel *channel;
  779. if (!sync_test_and_clear_bit(relid, recv_int_page))
  780. continue;
  781. /* Special case - vmbus channel protocol msg */
  782. if (relid == 0)
  783. continue;
  784. rcu_read_lock();
  785. /* Find channel based on relid */
  786. list_for_each_entry_rcu(channel, &hv_cpu->chan_list, percpu_list) {
  787. if (channel->offermsg.child_relid != relid)
  788. continue;
  789. switch (channel->callback_mode) {
  790. case HV_CALL_ISR:
  791. vmbus_channel_isr(channel);
  792. break;
  793. case HV_CALL_BATCHED:
  794. hv_begin_read(&channel->inbound);
  795. /* fallthrough */
  796. case HV_CALL_DIRECT:
  797. tasklet_schedule(&channel->callback_event);
  798. }
  799. }
  800. rcu_read_unlock();
  801. }
  802. }
  803. static void vmbus_isr(void)
  804. {
  805. struct hv_per_cpu_context *hv_cpu
  806. = this_cpu_ptr(hv_context.cpu_context);
  807. void *page_addr = hv_cpu->synic_event_page;
  808. struct hv_message *msg;
  809. union hv_synic_event_flags *event;
  810. bool handled = false;
  811. if (unlikely(page_addr == NULL))
  812. return;
  813. event = (union hv_synic_event_flags *)page_addr +
  814. VMBUS_MESSAGE_SINT;
  815. /*
  816. * Check for events before checking for messages. This is the order
  817. * in which events and messages are checked in Windows guests on
  818. * Hyper-V, and the Windows team suggested we do the same.
  819. */
  820. if ((vmbus_proto_version == VERSION_WS2008) ||
  821. (vmbus_proto_version == VERSION_WIN7)) {
  822. /* Since we are a child, we only need to check bit 0 */
  823. if (sync_test_and_clear_bit(0, event->flags))
  824. handled = true;
  825. } else {
  826. /*
  827. * Our host is win8 or above. The signaling mechanism
  828. * has changed and we can directly look at the event page.
  829. * If bit n is set then we have an interrup on the channel
  830. * whose id is n.
  831. */
  832. handled = true;
  833. }
  834. if (handled)
  835. vmbus_chan_sched(hv_cpu);
  836. page_addr = hv_cpu->synic_message_page;
  837. msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
  838. /* Check if there are actual msgs to be processed */
  839. if (msg->header.message_type != HVMSG_NONE) {
  840. if (msg->header.message_type == HVMSG_TIMER_EXPIRED)
  841. hv_process_timer_expiration(msg, hv_cpu);
  842. else
  843. tasklet_schedule(&hv_cpu->msg_dpc);
  844. }
  845. add_interrupt_randomness(HYPERVISOR_CALLBACK_VECTOR, 0);
  846. }
  847. /*
  848. * vmbus_bus_init -Main vmbus driver initialization routine.
  849. *
  850. * Here, we
  851. * - initialize the vmbus driver context
  852. * - invoke the vmbus hv main init routine
  853. * - retrieve the channel offers
  854. */
  855. static int vmbus_bus_init(void)
  856. {
  857. int ret;
  858. /* Hypervisor initialization...setup hypercall page..etc */
  859. ret = hv_init();
  860. if (ret != 0) {
  861. pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
  862. return ret;
  863. }
  864. ret = bus_register(&hv_bus);
  865. if (ret)
  866. return ret;
  867. hv_setup_vmbus_irq(vmbus_isr);
  868. ret = hv_synic_alloc();
  869. if (ret)
  870. goto err_alloc;
  871. /*
  872. * Initialize the per-cpu interrupt state and
  873. * connect to the host.
  874. */
  875. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv:online",
  876. hv_synic_init, hv_synic_cleanup);
  877. if (ret < 0)
  878. goto err_alloc;
  879. hyperv_cpuhp_online = ret;
  880. ret = vmbus_connect();
  881. if (ret)
  882. goto err_connect;
  883. /*
  884. * Only register if the crash MSRs are available
  885. */
  886. if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
  887. register_die_notifier(&hyperv_die_block);
  888. atomic_notifier_chain_register(&panic_notifier_list,
  889. &hyperv_panic_block);
  890. }
  891. vmbus_request_offers();
  892. return 0;
  893. err_connect:
  894. cpuhp_remove_state(hyperv_cpuhp_online);
  895. err_alloc:
  896. hv_synic_free();
  897. hv_remove_vmbus_irq();
  898. bus_unregister(&hv_bus);
  899. return ret;
  900. }
  901. /**
  902. * __vmbus_child_driver_register() - Register a vmbus's driver
  903. * @hv_driver: Pointer to driver structure you want to register
  904. * @owner: owner module of the drv
  905. * @mod_name: module name string
  906. *
  907. * Registers the given driver with Linux through the 'driver_register()' call
  908. * and sets up the hyper-v vmbus handling for this driver.
  909. * It will return the state of the 'driver_register()' call.
  910. *
  911. */
  912. int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name)
  913. {
  914. int ret;
  915. pr_info("registering driver %s\n", hv_driver->name);
  916. ret = vmbus_exists();
  917. if (ret < 0)
  918. return ret;
  919. hv_driver->driver.name = hv_driver->name;
  920. hv_driver->driver.owner = owner;
  921. hv_driver->driver.mod_name = mod_name;
  922. hv_driver->driver.bus = &hv_bus;
  923. spin_lock_init(&hv_driver->dynids.lock);
  924. INIT_LIST_HEAD(&hv_driver->dynids.list);
  925. ret = driver_register(&hv_driver->driver);
  926. return ret;
  927. }
  928. EXPORT_SYMBOL_GPL(__vmbus_driver_register);
  929. /**
  930. * vmbus_driver_unregister() - Unregister a vmbus's driver
  931. * @hv_driver: Pointer to driver structure you want to
  932. * un-register
  933. *
  934. * Un-register the given driver that was previous registered with a call to
  935. * vmbus_driver_register()
  936. */
  937. void vmbus_driver_unregister(struct hv_driver *hv_driver)
  938. {
  939. pr_info("unregistering driver %s\n", hv_driver->name);
  940. if (!vmbus_exists()) {
  941. driver_unregister(&hv_driver->driver);
  942. vmbus_free_dynids(hv_driver);
  943. }
  944. }
  945. EXPORT_SYMBOL_GPL(vmbus_driver_unregister);
  946. /*
  947. * vmbus_device_create - Creates and registers a new child device
  948. * on the vmbus.
  949. */
  950. struct hv_device *vmbus_device_create(const uuid_le *type,
  951. const uuid_le *instance,
  952. struct vmbus_channel *channel)
  953. {
  954. struct hv_device *child_device_obj;
  955. child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
  956. if (!child_device_obj) {
  957. pr_err("Unable to allocate device object for child device\n");
  958. return NULL;
  959. }
  960. child_device_obj->channel = channel;
  961. memcpy(&child_device_obj->dev_type, type, sizeof(uuid_le));
  962. memcpy(&child_device_obj->dev_instance, instance,
  963. sizeof(uuid_le));
  964. child_device_obj->vendor_id = 0x1414; /* MSFT vendor ID */
  965. return child_device_obj;
  966. }
  967. /*
  968. * vmbus_device_register - Register the child device
  969. */
  970. int vmbus_device_register(struct hv_device *child_device_obj)
  971. {
  972. int ret = 0;
  973. dev_set_name(&child_device_obj->device, "%pUl",
  974. child_device_obj->channel->offermsg.offer.if_instance.b);
  975. child_device_obj->device.bus = &hv_bus;
  976. child_device_obj->device.parent = &hv_acpi_dev->dev;
  977. child_device_obj->device.release = vmbus_device_release;
  978. /*
  979. * Register with the LDM. This will kick off the driver/device
  980. * binding...which will eventually call vmbus_match() and vmbus_probe()
  981. */
  982. ret = device_register(&child_device_obj->device);
  983. if (ret)
  984. pr_err("Unable to register child device\n");
  985. else
  986. pr_debug("child device %s registered\n",
  987. dev_name(&child_device_obj->device));
  988. return ret;
  989. }
  990. /*
  991. * vmbus_device_unregister - Remove the specified child device
  992. * from the vmbus.
  993. */
  994. void vmbus_device_unregister(struct hv_device *device_obj)
  995. {
  996. pr_debug("child device %s unregistered\n",
  997. dev_name(&device_obj->device));
  998. /*
  999. * Kick off the process of unregistering the device.
  1000. * This will call vmbus_remove() and eventually vmbus_device_release()
  1001. */
  1002. device_unregister(&device_obj->device);
  1003. }
  1004. /*
  1005. * VMBUS is an acpi enumerated device. Get the information we
  1006. * need from DSDT.
  1007. */
  1008. #define VTPM_BASE_ADDRESS 0xfed40000
  1009. static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *ctx)
  1010. {
  1011. resource_size_t start = 0;
  1012. resource_size_t end = 0;
  1013. struct resource *new_res;
  1014. struct resource **old_res = &hyperv_mmio;
  1015. struct resource **prev_res = NULL;
  1016. switch (res->type) {
  1017. /*
  1018. * "Address" descriptors are for bus windows. Ignore
  1019. * "memory" descriptors, which are for registers on
  1020. * devices.
  1021. */
  1022. case ACPI_RESOURCE_TYPE_ADDRESS32:
  1023. start = res->data.address32.address.minimum;
  1024. end = res->data.address32.address.maximum;
  1025. break;
  1026. case ACPI_RESOURCE_TYPE_ADDRESS64:
  1027. start = res->data.address64.address.minimum;
  1028. end = res->data.address64.address.maximum;
  1029. break;
  1030. default:
  1031. /* Unused resource type */
  1032. return AE_OK;
  1033. }
  1034. /*
  1035. * Ignore ranges that are below 1MB, as they're not
  1036. * necessary or useful here.
  1037. */
  1038. if (end < 0x100000)
  1039. return AE_OK;
  1040. new_res = kzalloc(sizeof(*new_res), GFP_ATOMIC);
  1041. if (!new_res)
  1042. return AE_NO_MEMORY;
  1043. /* If this range overlaps the virtual TPM, truncate it. */
  1044. if (end > VTPM_BASE_ADDRESS && start < VTPM_BASE_ADDRESS)
  1045. end = VTPM_BASE_ADDRESS;
  1046. new_res->name = "hyperv mmio";
  1047. new_res->flags = IORESOURCE_MEM;
  1048. new_res->start = start;
  1049. new_res->end = end;
  1050. /*
  1051. * If two ranges are adjacent, merge them.
  1052. */
  1053. do {
  1054. if (!*old_res) {
  1055. *old_res = new_res;
  1056. break;
  1057. }
  1058. if (((*old_res)->end + 1) == new_res->start) {
  1059. (*old_res)->end = new_res->end;
  1060. kfree(new_res);
  1061. break;
  1062. }
  1063. if ((*old_res)->start == new_res->end + 1) {
  1064. (*old_res)->start = new_res->start;
  1065. kfree(new_res);
  1066. break;
  1067. }
  1068. if ((*old_res)->start > new_res->end) {
  1069. new_res->sibling = *old_res;
  1070. if (prev_res)
  1071. (*prev_res)->sibling = new_res;
  1072. *old_res = new_res;
  1073. break;
  1074. }
  1075. prev_res = old_res;
  1076. old_res = &(*old_res)->sibling;
  1077. } while (1);
  1078. return AE_OK;
  1079. }
  1080. static int vmbus_acpi_remove(struct acpi_device *device)
  1081. {
  1082. struct resource *cur_res;
  1083. struct resource *next_res;
  1084. if (hyperv_mmio) {
  1085. if (fb_mmio) {
  1086. __release_region(hyperv_mmio, fb_mmio->start,
  1087. resource_size(fb_mmio));
  1088. fb_mmio = NULL;
  1089. }
  1090. for (cur_res = hyperv_mmio; cur_res; cur_res = next_res) {
  1091. next_res = cur_res->sibling;
  1092. kfree(cur_res);
  1093. }
  1094. }
  1095. return 0;
  1096. }
  1097. static void vmbus_reserve_fb(void)
  1098. {
  1099. int size;
  1100. /*
  1101. * Make a claim for the frame buffer in the resource tree under the
  1102. * first node, which will be the one below 4GB. The length seems to
  1103. * be underreported, particularly in a Generation 1 VM. So start out
  1104. * reserving a larger area and make it smaller until it succeeds.
  1105. */
  1106. if (screen_info.lfb_base) {
  1107. if (efi_enabled(EFI_BOOT))
  1108. size = max_t(__u32, screen_info.lfb_size, 0x800000);
  1109. else
  1110. size = max_t(__u32, screen_info.lfb_size, 0x4000000);
  1111. for (; !fb_mmio && (size >= 0x100000); size >>= 1) {
  1112. fb_mmio = __request_region(hyperv_mmio,
  1113. screen_info.lfb_base, size,
  1114. fb_mmio_name, 0);
  1115. }
  1116. }
  1117. }
  1118. /**
  1119. * vmbus_allocate_mmio() - Pick a memory-mapped I/O range.
  1120. * @new: If successful, supplied a pointer to the
  1121. * allocated MMIO space.
  1122. * @device_obj: Identifies the caller
  1123. * @min: Minimum guest physical address of the
  1124. * allocation
  1125. * @max: Maximum guest physical address
  1126. * @size: Size of the range to be allocated
  1127. * @align: Alignment of the range to be allocated
  1128. * @fb_overlap_ok: Whether this allocation can be allowed
  1129. * to overlap the video frame buffer.
  1130. *
  1131. * This function walks the resources granted to VMBus by the
  1132. * _CRS object in the ACPI namespace underneath the parent
  1133. * "bridge" whether that's a root PCI bus in the Generation 1
  1134. * case or a Module Device in the Generation 2 case. It then
  1135. * attempts to allocate from the global MMIO pool in a way that
  1136. * matches the constraints supplied in these parameters and by
  1137. * that _CRS.
  1138. *
  1139. * Return: 0 on success, -errno on failure
  1140. */
  1141. int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj,
  1142. resource_size_t min, resource_size_t max,
  1143. resource_size_t size, resource_size_t align,
  1144. bool fb_overlap_ok)
  1145. {
  1146. struct resource *iter, *shadow;
  1147. resource_size_t range_min, range_max, start;
  1148. const char *dev_n = dev_name(&device_obj->device);
  1149. int retval;
  1150. retval = -ENXIO;
  1151. down(&hyperv_mmio_lock);
  1152. /*
  1153. * If overlaps with frame buffers are allowed, then first attempt to
  1154. * make the allocation from within the reserved region. Because it
  1155. * is already reserved, no shadow allocation is necessary.
  1156. */
  1157. if (fb_overlap_ok && fb_mmio && !(min > fb_mmio->end) &&
  1158. !(max < fb_mmio->start)) {
  1159. range_min = fb_mmio->start;
  1160. range_max = fb_mmio->end;
  1161. start = (range_min + align - 1) & ~(align - 1);
  1162. for (; start + size - 1 <= range_max; start += align) {
  1163. *new = request_mem_region_exclusive(start, size, dev_n);
  1164. if (*new) {
  1165. retval = 0;
  1166. goto exit;
  1167. }
  1168. }
  1169. }
  1170. for (iter = hyperv_mmio; iter; iter = iter->sibling) {
  1171. if ((iter->start >= max) || (iter->end <= min))
  1172. continue;
  1173. range_min = iter->start;
  1174. range_max = iter->end;
  1175. start = (range_min + align - 1) & ~(align - 1);
  1176. for (; start + size - 1 <= range_max; start += align) {
  1177. shadow = __request_region(iter, start, size, NULL,
  1178. IORESOURCE_BUSY);
  1179. if (!shadow)
  1180. continue;
  1181. *new = request_mem_region_exclusive(start, size, dev_n);
  1182. if (*new) {
  1183. shadow->name = (char *)*new;
  1184. retval = 0;
  1185. goto exit;
  1186. }
  1187. __release_region(iter, start, size);
  1188. }
  1189. }
  1190. exit:
  1191. up(&hyperv_mmio_lock);
  1192. return retval;
  1193. }
  1194. EXPORT_SYMBOL_GPL(vmbus_allocate_mmio);
  1195. /**
  1196. * vmbus_free_mmio() - Free a memory-mapped I/O range.
  1197. * @start: Base address of region to release.
  1198. * @size: Size of the range to be allocated
  1199. *
  1200. * This function releases anything requested by
  1201. * vmbus_mmio_allocate().
  1202. */
  1203. void vmbus_free_mmio(resource_size_t start, resource_size_t size)
  1204. {
  1205. struct resource *iter;
  1206. down(&hyperv_mmio_lock);
  1207. for (iter = hyperv_mmio; iter; iter = iter->sibling) {
  1208. if ((iter->start >= start + size) || (iter->end <= start))
  1209. continue;
  1210. __release_region(iter, start, size);
  1211. }
  1212. release_mem_region(start, size);
  1213. up(&hyperv_mmio_lock);
  1214. }
  1215. EXPORT_SYMBOL_GPL(vmbus_free_mmio);
  1216. /**
  1217. * vmbus_cpu_number_to_vp_number() - Map CPU to VP.
  1218. * @cpu_number: CPU number in Linux terms
  1219. *
  1220. * This function returns the mapping between the Linux processor
  1221. * number and the hypervisor's virtual processor number, useful
  1222. * in making hypercalls and such that talk about specific
  1223. * processors.
  1224. *
  1225. * Return: Virtual processor number in Hyper-V terms
  1226. */
  1227. int vmbus_cpu_number_to_vp_number(int cpu_number)
  1228. {
  1229. return hv_context.vp_index[cpu_number];
  1230. }
  1231. EXPORT_SYMBOL_GPL(vmbus_cpu_number_to_vp_number);
  1232. static int vmbus_acpi_add(struct acpi_device *device)
  1233. {
  1234. acpi_status result;
  1235. int ret_val = -ENODEV;
  1236. struct acpi_device *ancestor;
  1237. hv_acpi_dev = device;
  1238. result = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
  1239. vmbus_walk_resources, NULL);
  1240. if (ACPI_FAILURE(result))
  1241. goto acpi_walk_err;
  1242. /*
  1243. * Some ancestor of the vmbus acpi device (Gen1 or Gen2
  1244. * firmware) is the VMOD that has the mmio ranges. Get that.
  1245. */
  1246. for (ancestor = device->parent; ancestor; ancestor = ancestor->parent) {
  1247. result = acpi_walk_resources(ancestor->handle, METHOD_NAME__CRS,
  1248. vmbus_walk_resources, NULL);
  1249. if (ACPI_FAILURE(result))
  1250. continue;
  1251. if (hyperv_mmio) {
  1252. vmbus_reserve_fb();
  1253. break;
  1254. }
  1255. }
  1256. ret_val = 0;
  1257. acpi_walk_err:
  1258. complete(&probe_event);
  1259. if (ret_val)
  1260. vmbus_acpi_remove(device);
  1261. return ret_val;
  1262. }
  1263. static const struct acpi_device_id vmbus_acpi_device_ids[] = {
  1264. {"VMBUS", 0},
  1265. {"VMBus", 0},
  1266. {"", 0},
  1267. };
  1268. MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
  1269. static struct acpi_driver vmbus_acpi_driver = {
  1270. .name = "vmbus",
  1271. .ids = vmbus_acpi_device_ids,
  1272. .ops = {
  1273. .add = vmbus_acpi_add,
  1274. .remove = vmbus_acpi_remove,
  1275. },
  1276. };
  1277. static void hv_kexec_handler(void)
  1278. {
  1279. hv_synic_clockevents_cleanup();
  1280. vmbus_initiate_unload(false);
  1281. vmbus_connection.conn_state = DISCONNECTED;
  1282. /* Make sure conn_state is set as hv_synic_cleanup checks for it */
  1283. mb();
  1284. cpuhp_remove_state(hyperv_cpuhp_online);
  1285. hyperv_cleanup();
  1286. };
  1287. static void hv_crash_handler(struct pt_regs *regs)
  1288. {
  1289. vmbus_initiate_unload(true);
  1290. /*
  1291. * In crash handler we can't schedule synic cleanup for all CPUs,
  1292. * doing the cleanup for current CPU only. This should be sufficient
  1293. * for kdump.
  1294. */
  1295. vmbus_connection.conn_state = DISCONNECTED;
  1296. hv_synic_cleanup(smp_processor_id());
  1297. hyperv_cleanup();
  1298. };
  1299. static int __init hv_acpi_init(void)
  1300. {
  1301. int ret, t;
  1302. if (x86_hyper != &x86_hyper_ms_hyperv)
  1303. return -ENODEV;
  1304. init_completion(&probe_event);
  1305. /*
  1306. * Get ACPI resources first.
  1307. */
  1308. ret = acpi_bus_register_driver(&vmbus_acpi_driver);
  1309. if (ret)
  1310. return ret;
  1311. t = wait_for_completion_timeout(&probe_event, 5*HZ);
  1312. if (t == 0) {
  1313. ret = -ETIMEDOUT;
  1314. goto cleanup;
  1315. }
  1316. ret = vmbus_bus_init();
  1317. if (ret)
  1318. goto cleanup;
  1319. hv_setup_kexec_handler(hv_kexec_handler);
  1320. hv_setup_crash_handler(hv_crash_handler);
  1321. return 0;
  1322. cleanup:
  1323. acpi_bus_unregister_driver(&vmbus_acpi_driver);
  1324. hv_acpi_dev = NULL;
  1325. return ret;
  1326. }
  1327. static void __exit vmbus_exit(void)
  1328. {
  1329. int cpu;
  1330. hv_remove_kexec_handler();
  1331. hv_remove_crash_handler();
  1332. vmbus_connection.conn_state = DISCONNECTED;
  1333. hv_synic_clockevents_cleanup();
  1334. vmbus_disconnect();
  1335. hv_remove_vmbus_irq();
  1336. for_each_online_cpu(cpu) {
  1337. struct hv_per_cpu_context *hv_cpu
  1338. = per_cpu_ptr(hv_context.cpu_context, cpu);
  1339. tasklet_kill(&hv_cpu->msg_dpc);
  1340. }
  1341. vmbus_free_channels();
  1342. if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
  1343. unregister_die_notifier(&hyperv_die_block);
  1344. atomic_notifier_chain_unregister(&panic_notifier_list,
  1345. &hyperv_panic_block);
  1346. }
  1347. bus_unregister(&hv_bus);
  1348. cpuhp_remove_state(hyperv_cpuhp_online);
  1349. hv_synic_free();
  1350. acpi_bus_unregister_driver(&vmbus_acpi_driver);
  1351. }
  1352. MODULE_LICENSE("GPL");
  1353. subsys_initcall(hv_acpi_init);
  1354. module_exit(vmbus_exit);