Commit | Line | Data |
---|---|---|
e3b3d0f5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
b536b4b9 JF |
2 | /* |
3 | * xen console driver interface to hvc_console.c | |
4 | * | |
5 | * (c) 2007 Gerd Hoffmann <kraxel@suse.de> | |
b536b4b9 JF |
6 | */ |
7 | ||
8 | #include <linux/console.h> | |
9 | #include <linux/delay.h> | |
10 | #include <linux/err.h> | |
4d9310e3 | 11 | #include <linux/irq.h> |
b536b4b9 JF |
12 | #include <linux/init.h> |
13 | #include <linux/types.h> | |
02e19f9c | 14 | #include <linux/list.h> |
16e506ef | 15 | #include <linux/serial_core.h> |
b536b4b9 | 16 | |
eb5ef071 | 17 | #include <asm/io.h> |
b536b4b9 | 18 | #include <asm/xen/hypervisor.h> |
1ccbf534 JF |
19 | |
20 | #include <xen/xen.h> | |
eb5ef071 SS |
21 | #include <xen/interface/xen.h> |
22 | #include <xen/hvm.h> | |
02e19f9c | 23 | #include <xen/grant_table.h> |
b536b4b9 JF |
24 | #include <xen/page.h> |
25 | #include <xen/events.h> | |
26 | #include <xen/interface/io/console.h> | |
4d9310e3 | 27 | #include <xen/interface/sched.h> |
b536b4b9 | 28 | #include <xen/hvc-console.h> |
02e19f9c | 29 | #include <xen/xenbus.h> |
b536b4b9 JF |
30 | |
31 | #include "hvc_console.h" | |
32 | ||
33 | #define HVC_COOKIE 0x58656e /* "Xen" in hex */ | |
34 | ||
02e19f9c SS |
35 | struct xencons_info { |
36 | struct list_head list; | |
37 | struct xenbus_device *xbdev; | |
38 | struct xencons_interface *intf; | |
39 | unsigned int evtchn; | |
fe415186 JG |
40 | XENCONS_RING_IDX out_cons; |
41 | unsigned int out_cons_same; | |
02e19f9c SS |
42 | struct hvc_struct *hvc; |
43 | int irq; | |
44 | int vtermno; | |
45 | grant_ref_t gntref; | |
46 | }; | |
47 | ||
48 | static LIST_HEAD(xenconsoles); | |
49 | static DEFINE_SPINLOCK(xencons_lock); | |
b536b4b9 JF |
50 | |
51 | /* ------------------------------------------------------------------ */ | |
52 | ||
02e19f9c SS |
53 | static struct xencons_info *vtermno_to_xencons(int vtermno) |
54 | { | |
c0dccad8 RPM |
55 | struct xencons_info *entry, *ret = NULL; |
56 | unsigned long flags; | |
02e19f9c | 57 | |
c0dccad8 RPM |
58 | spin_lock_irqsave(&xencons_lock, flags); |
59 | if (list_empty(&xenconsoles)) { | |
60 | spin_unlock_irqrestore(&xencons_lock, flags); | |
61 | return NULL; | |
62 | } | |
02e19f9c | 63 | |
c0dccad8 | 64 | list_for_each_entry(entry, &xenconsoles, list) { |
02e19f9c SS |
65 | if (entry->vtermno == vtermno) { |
66 | ret = entry; | |
67 | break; | |
68 | } | |
69 | } | |
c0dccad8 | 70 | spin_unlock_irqrestore(&xencons_lock, flags); |
6b9b732d | 71 | |
02e19f9c SS |
72 | return ret; |
73 | } | |
74 | ||
75 | static inline int xenbus_devid_to_vtermno(int devid) | |
b536b4b9 | 76 | { |
02e19f9c | 77 | return devid + HVC_COOKIE; |
b536b4b9 JF |
78 | } |
79 | ||
02e19f9c | 80 | static inline void notify_daemon(struct xencons_info *cons) |
b536b4b9 JF |
81 | { |
82 | /* Use evtchn: this is called early, before irq is set up. */ | |
02e19f9c | 83 | notify_remote_via_evtchn(cons->evtchn); |
b536b4b9 JF |
84 | } |
85 | ||
02e19f9c SS |
86 | static int __write_console(struct xencons_info *xencons, |
87 | const char *data, int len) | |
b536b4b9 | 88 | { |
b536b4b9 | 89 | XENCONS_RING_IDX cons, prod; |
02e19f9c | 90 | struct xencons_interface *intf = xencons->intf; |
b536b4b9 JF |
91 | int sent = 0; |
92 | ||
93 | cons = intf->out_cons; | |
94 | prod = intf->out_prod; | |
95 | mb(); /* update queue values before going on */ | |
e679004d JG |
96 | |
97 | if ((prod - cons) > sizeof(intf->out)) { | |
98 | pr_err_once("xencons: Illegal ring page indices"); | |
99 | return -EINVAL; | |
100 | } | |
b536b4b9 JF |
101 | |
102 | while ((sent < len) && ((prod - cons) < sizeof(intf->out))) | |
103 | intf->out[MASK_XENCONS_IDX(prod++, intf->out)] = data[sent++]; | |
104 | ||
105 | wmb(); /* write ring before updating pointer */ | |
106 | intf->out_prod = prod; | |
107 | ||
403a85ff | 108 | if (sent) |
02e19f9c | 109 | notify_daemon(xencons); |
b536b4b9 JF |
110 | return sent; |
111 | } | |
112 | ||
4fe7d5a7 | 113 | static int domU_write_console(uint32_t vtermno, const char *data, int len) |
7825cf10 JF |
114 | { |
115 | int ret = len; | |
02e19f9c SS |
116 | struct xencons_info *cons = vtermno_to_xencons(vtermno); |
117 | if (cons == NULL) | |
118 | return -EINVAL; | |
7825cf10 JF |
119 | |
120 | /* | |
121 | * Make sure the whole buffer is emitted, polling if | |
122 | * necessary. We don't ever want to rely on the hvc daemon | |
123 | * because the most interesting console output is when the | |
124 | * kernel is crippled. | |
125 | */ | |
126 | while (len) { | |
02e19f9c | 127 | int sent = __write_console(cons, data, len); |
e679004d JG |
128 | |
129 | if (sent < 0) | |
130 | return sent; | |
131 | ||
7825cf10 JF |
132 | data += sent; |
133 | len -= sent; | |
134 | ||
135 | if (unlikely(len)) | |
136 | HYPERVISOR_sched_op(SCHEDOP_yield, NULL); | |
137 | } | |
138 | ||
139 | return ret; | |
140 | } | |
141 | ||
4fe7d5a7 | 142 | static int domU_read_console(uint32_t vtermno, char *buf, int len) |
b536b4b9 | 143 | { |
02e19f9c | 144 | struct xencons_interface *intf; |
b536b4b9 JF |
145 | XENCONS_RING_IDX cons, prod; |
146 | int recv = 0; | |
02e19f9c | 147 | struct xencons_info *xencons = vtermno_to_xencons(vtermno); |
fe415186 JG |
148 | unsigned int eoiflag = 0; |
149 | ||
02e19f9c SS |
150 | if (xencons == NULL) |
151 | return -EINVAL; | |
152 | intf = xencons->intf; | |
b536b4b9 JF |
153 | |
154 | cons = intf->in_cons; | |
155 | prod = intf->in_prod; | |
156 | mb(); /* get pointers before reading ring */ | |
e679004d JG |
157 | |
158 | if ((prod - cons) > sizeof(intf->in)) { | |
159 | pr_err_once("xencons: Illegal ring page indices"); | |
160 | return -EINVAL; | |
161 | } | |
b536b4b9 JF |
162 | |
163 | while (cons != prod && recv < len) | |
164 | buf[recv++] = intf->in[MASK_XENCONS_IDX(cons++, intf->in)]; | |
165 | ||
166 | mb(); /* read ring before consuming */ | |
167 | intf->in_cons = cons; | |
168 | ||
fe415186 JG |
169 | /* |
170 | * When to mark interrupt having been spurious: | |
171 | * - there was no new data to be read, and | |
172 | * - the backend did not consume some output bytes, and | |
173 | * - the previous round with no read data didn't see consumed bytes | |
174 | * (we might have a race with an interrupt being in flight while | |
175 | * updating xencons->out_cons, so account for that by allowing one | |
176 | * round without any visible reason) | |
177 | */ | |
178 | if (intf->out_cons != xencons->out_cons) { | |
179 | xencons->out_cons = intf->out_cons; | |
180 | xencons->out_cons_same = 0; | |
181 | } | |
182 | if (recv) { | |
183 | notify_daemon(xencons); | |
184 | } else if (xencons->out_cons_same++ > 1) { | |
185 | eoiflag = XEN_EOI_FLAG_SPURIOUS; | |
186 | } | |
187 | ||
188 | xen_irq_lateeoi(xencons->irq, eoiflag); | |
189 | ||
b536b4b9 JF |
190 | return recv; |
191 | } | |
192 | ||
54573c4a | 193 | static const struct hv_ops domU_hvc_ops = { |
4fe7d5a7 JF |
194 | .get_chars = domU_read_console, |
195 | .put_chars = domU_write_console, | |
611e097d CB |
196 | .notifier_add = notifier_add_irq, |
197 | .notifier_del = notifier_del_irq, | |
fc362e2e | 198 | .notifier_hangup = notifier_hangup_irq, |
b536b4b9 JF |
199 | }; |
200 | ||
4fe7d5a7 JF |
201 | static int dom0_read_console(uint32_t vtermno, char *buf, int len) |
202 | { | |
203 | return HYPERVISOR_console_io(CONSOLEIO_read, len, buf); | |
204 | } | |
205 | ||
206 | /* | |
207 | * Either for a dom0 to write to the system console, or a domU with a | |
208 | * debug version of Xen | |
209 | */ | |
210 | static int dom0_write_console(uint32_t vtermno, const char *str, int len) | |
211 | { | |
212 | int rc = HYPERVISOR_console_io(CONSOLEIO_write, len, (char *)str); | |
213 | if (rc < 0) | |
04b772d2 | 214 | return rc; |
4fe7d5a7 JF |
215 | |
216 | return len; | |
217 | } | |
218 | ||
54573c4a | 219 | static const struct hv_ops dom0_hvc_ops = { |
4fe7d5a7 JF |
220 | .get_chars = dom0_read_console, |
221 | .put_chars = dom0_write_console, | |
222 | .notifier_add = notifier_add_irq, | |
223 | .notifier_del = notifier_del_irq, | |
224 | .notifier_hangup = notifier_hangup_irq, | |
225 | }; | |
226 | ||
eb5ef071 SS |
227 | static int xen_hvm_console_init(void) |
228 | { | |
229 | int r; | |
230 | uint64_t v = 0; | |
c0dccad8 | 231 | unsigned long gfn, flags; |
02e19f9c | 232 | struct xencons_info *info; |
eb5ef071 SS |
233 | |
234 | if (!xen_hvm_domain()) | |
235 | return -ENODEV; | |
236 | ||
02e19f9c SS |
237 | info = vtermno_to_xencons(HVC_COOKIE); |
238 | if (!info) { | |
2d1d3f3a | 239 | info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL); |
02e19f9c SS |
240 | if (!info) |
241 | return -ENOMEM; | |
37a80bf5 KRW |
242 | } else if (info->intf != NULL) { |
243 | /* already configured */ | |
02e19f9c | 244 | return 0; |
37a80bf5 | 245 | } |
5842f576 KRW |
246 | /* |
247 | * If the toolstack (or the hypervisor) hasn't set these values, the | |
859e3267 | 248 | * default value is 0. Even though gfn = 0 and evtchn = 0 are |
5842f576 KRW |
249 | * theoretically correct values, in practice they never are and they |
250 | * mean that a legacy toolstack hasn't initialized the pv console correctly. | |
251 | */ | |
eb5ef071 | 252 | r = hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN, &v); |
5842f576 | 253 | if (r < 0 || v == 0) |
2e5ad6b9 | 254 | goto err; |
02e19f9c | 255 | info->evtchn = v; |
a32c88b9 KRW |
256 | v = 0; |
257 | r = hvm_get_parameter(HVM_PARAM_CONSOLE_PFN, &v); | |
5842f576 | 258 | if (r < 0 || v == 0) |
2e5ad6b9 | 259 | goto err; |
859e3267 | 260 | gfn = v; |
41925b10 | 261 | info->intf = memremap(gfn << XEN_PAGE_SHIFT, XEN_PAGE_SIZE, MEMREMAP_WB); |
2e5ad6b9 KRW |
262 | if (info->intf == NULL) |
263 | goto err; | |
02e19f9c SS |
264 | info->vtermno = HVC_COOKIE; |
265 | ||
c0dccad8 | 266 | spin_lock_irqsave(&xencons_lock, flags); |
02e19f9c | 267 | list_add_tail(&info->list, &xenconsoles); |
c0dccad8 | 268 | spin_unlock_irqrestore(&xencons_lock, flags); |
02e19f9c SS |
269 | |
270 | return 0; | |
2e5ad6b9 KRW |
271 | err: |
272 | kfree(info); | |
273 | return -ENODEV; | |
02e19f9c SS |
274 | } |
275 | ||
5de738b3 SS |
276 | static int xencons_info_pv_init(struct xencons_info *info, int vtermno) |
277 | { | |
278 | info->evtchn = xen_start_info->console.domU.evtchn; | |
279 | /* GFN == MFN for PV guest */ | |
280 | info->intf = gfn_to_virt(xen_start_info->console.domU.mfn); | |
281 | info->vtermno = vtermno; | |
282 | ||
283 | list_add_tail(&info->list, &xenconsoles); | |
284 | ||
285 | return 0; | |
286 | } | |
287 | ||
02e19f9c SS |
288 | static int xen_pv_console_init(void) |
289 | { | |
290 | struct xencons_info *info; | |
c0dccad8 | 291 | unsigned long flags; |
02e19f9c SS |
292 | |
293 | if (!xen_pv_domain()) | |
294 | return -ENODEV; | |
295 | ||
296 | if (!xen_start_info->console.domU.evtchn) | |
297 | return -ENODEV; | |
298 | ||
299 | info = vtermno_to_xencons(HVC_COOKIE); | |
300 | if (!info) { | |
2d1d3f3a | 301 | info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL); |
02e19f9c SS |
302 | if (!info) |
303 | return -ENOMEM; | |
37a80bf5 KRW |
304 | } else if (info->intf != NULL) { |
305 | /* already configured */ | |
02e19f9c | 306 | return 0; |
37a80bf5 | 307 | } |
c0dccad8 | 308 | spin_lock_irqsave(&xencons_lock, flags); |
5de738b3 | 309 | xencons_info_pv_init(info, HVC_COOKIE); |
c0dccad8 | 310 | spin_unlock_irqrestore(&xencons_lock, flags); |
02e19f9c SS |
311 | |
312 | return 0; | |
313 | } | |
314 | ||
315 | static int xen_initial_domain_console_init(void) | |
316 | { | |
317 | struct xencons_info *info; | |
c0dccad8 | 318 | unsigned long flags; |
02e19f9c SS |
319 | |
320 | if (!xen_initial_domain()) | |
321 | return -ENODEV; | |
322 | ||
323 | info = vtermno_to_xencons(HVC_COOKIE); | |
324 | if (!info) { | |
2d1d3f3a | 325 | info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL); |
02e19f9c SS |
326 | if (!info) |
327 | return -ENOMEM; | |
328 | } | |
329 | ||
77bb3dfd | 330 | info->irq = bind_virq_to_irq(VIRQ_CONSOLE, 0, false); |
02e19f9c SS |
331 | info->vtermno = HVC_COOKIE; |
332 | ||
c0dccad8 | 333 | spin_lock_irqsave(&xencons_lock, flags); |
02e19f9c | 334 | list_add_tail(&info->list, &xenconsoles); |
c0dccad8 | 335 | spin_unlock_irqrestore(&xencons_lock, flags); |
eb5ef071 SS |
336 | |
337 | return 0; | |
338 | } | |
339 | ||
b9d934f2 BO |
340 | static void xen_console_update_evtchn(struct xencons_info *info) |
341 | { | |
342 | if (xen_hvm_domain()) { | |
c4ace5da | 343 | uint64_t v = 0; |
b9d934f2 BO |
344 | int err; |
345 | ||
346 | err = hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN, &v); | |
347 | if (!err && v) | |
348 | info->evtchn = v; | |
349 | } else | |
350 | info->evtchn = xen_start_info->console.domU.evtchn; | |
351 | } | |
352 | ||
02e19f9c SS |
353 | void xen_console_resume(void) |
354 | { | |
355 | struct xencons_info *info = vtermno_to_xencons(HVC_COOKIE); | |
b9d934f2 BO |
356 | if (info != NULL && info->irq) { |
357 | if (!xen_initial_domain()) | |
358 | xen_console_update_evtchn(info); | |
02e19f9c | 359 | rebind_evtchn_irq(info->evtchn, info->irq); |
b9d934f2 | 360 | } |
02e19f9c SS |
361 | } |
362 | ||
e36ae343 | 363 | #ifdef CONFIG_HVC_XEN_FRONTEND |
02e19f9c SS |
364 | static void xencons_disconnect_backend(struct xencons_info *info) |
365 | { | |
366 | if (info->irq > 0) | |
367 | unbind_from_irqhandler(info->irq, NULL); | |
368 | info->irq = 0; | |
369 | if (info->evtchn > 0) | |
370 | xenbus_free_evtchn(info->xbdev, info->evtchn); | |
371 | info->evtchn = 0; | |
372 | if (info->gntref > 0) | |
373 | gnttab_free_grant_references(info->gntref); | |
374 | info->gntref = 0; | |
375 | if (info->hvc != NULL) | |
376 | hvc_remove(info->hvc); | |
377 | info->hvc = NULL; | |
378 | } | |
379 | ||
380 | static void xencons_free(struct xencons_info *info) | |
381 | { | |
382 | free_page((unsigned long)info->intf); | |
383 | info->intf = NULL; | |
384 | info->vtermno = 0; | |
385 | kfree(info); | |
386 | } | |
387 | ||
388 | static int xen_console_remove(struct xencons_info *info) | |
389 | { | |
c0dccad8 RPM |
390 | unsigned long flags; |
391 | ||
02e19f9c | 392 | xencons_disconnect_backend(info); |
c0dccad8 | 393 | spin_lock_irqsave(&xencons_lock, flags); |
02e19f9c | 394 | list_del(&info->list); |
c0dccad8 | 395 | spin_unlock_irqrestore(&xencons_lock, flags); |
02e19f9c SS |
396 | if (info->xbdev != NULL) |
397 | xencons_free(info); | |
398 | else { | |
399 | if (xen_hvm_domain()) | |
400 | iounmap(info->intf); | |
401 | kfree(info); | |
4fe7d5a7 | 402 | } |
02e19f9c SS |
403 | return 0; |
404 | } | |
6b9b732d | 405 | |
7cffcade | 406 | static void xencons_remove(struct xenbus_device *dev) |
02e19f9c | 407 | { |
7cffcade | 408 | xen_console_remove(dev_get_drvdata(&dev->dev)); |
02e19f9c | 409 | } |
b536b4b9 | 410 | |
02e19f9c SS |
411 | static int xencons_connect_backend(struct xenbus_device *dev, |
412 | struct xencons_info *info) | |
413 | { | |
414 | int ret, evtchn, devid, ref, irq; | |
415 | struct xenbus_transaction xbt; | |
416 | grant_ref_t gref_head; | |
02e19f9c SS |
417 | |
418 | ret = xenbus_alloc_evtchn(dev, &evtchn); | |
419 | if (ret) | |
420 | return ret; | |
421 | info->evtchn = evtchn; | |
fe415186 | 422 | irq = bind_interdomain_evtchn_to_irq_lateeoi(dev, evtchn); |
02e19f9c SS |
423 | if (irq < 0) |
424 | return irq; | |
425 | info->irq = irq; | |
426 | devid = dev->nodename[strlen(dev->nodename) - 1] - '0'; | |
427 | info->hvc = hvc_alloc(xenbus_devid_to_vtermno(devid), | |
428 | irq, &domU_hvc_ops, 256); | |
429 | if (IS_ERR(info->hvc)) | |
430 | return PTR_ERR(info->hvc); | |
02e19f9c SS |
431 | ret = gnttab_alloc_grant_references(1, &gref_head); |
432 | if (ret < 0) | |
433 | return ret; | |
434 | info->gntref = gref_head; | |
435 | ref = gnttab_claim_grant_reference(&gref_head); | |
436 | if (ref < 0) | |
437 | return ref; | |
438 | gnttab_grant_foreign_access_ref(ref, info->xbdev->otherend_id, | |
859e3267 | 439 | virt_to_gfn(info->intf), 0); |
02e19f9c SS |
440 | |
441 | again: | |
442 | ret = xenbus_transaction_start(&xbt); | |
443 | if (ret) { | |
444 | xenbus_dev_fatal(dev, ret, "starting transaction"); | |
445 | return ret; | |
446 | } | |
447 | ret = xenbus_printf(xbt, dev->nodename, "ring-ref", "%d", ref); | |
448 | if (ret) | |
449 | goto error_xenbus; | |
450 | ret = xenbus_printf(xbt, dev->nodename, "port", "%u", | |
451 | evtchn); | |
02e19f9c SS |
452 | if (ret) |
453 | goto error_xenbus; | |
454 | ret = xenbus_transaction_end(xbt, 0); | |
455 | if (ret) { | |
456 | if (ret == -EAGAIN) | |
457 | goto again; | |
458 | xenbus_dev_fatal(dev, ret, "completing transaction"); | |
459 | return ret; | |
460 | } | |
6b9b732d | 461 | |
02e19f9c | 462 | xenbus_switch_state(dev, XenbusStateInitialised); |
b536b4b9 | 463 | return 0; |
02e19f9c SS |
464 | |
465 | error_xenbus: | |
466 | xenbus_transaction_end(xbt, 1); | |
467 | xenbus_dev_fatal(dev, ret, "writing xenstore"); | |
468 | return ret; | |
b536b4b9 JF |
469 | } |
470 | ||
9671f099 | 471 | static int xencons_probe(struct xenbus_device *dev, |
02e19f9c | 472 | const struct xenbus_device_id *id) |
6b9b732d | 473 | { |
02e19f9c SS |
474 | int ret, devid; |
475 | struct xencons_info *info; | |
c0dccad8 | 476 | unsigned long flags; |
02e19f9c SS |
477 | |
478 | devid = dev->nodename[strlen(dev->nodename) - 1] - '0'; | |
479 | if (devid == 0) | |
480 | return -ENODEV; | |
481 | ||
201a52be | 482 | info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL); |
02e19f9c | 483 | if (!info) |
201a52be | 484 | return -ENOMEM; |
02e19f9c SS |
485 | dev_set_drvdata(&dev->dev, info); |
486 | info->xbdev = dev; | |
487 | info->vtermno = xenbus_devid_to_vtermno(devid); | |
488 | info->intf = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO); | |
489 | if (!info->intf) | |
490 | goto error_nomem; | |
491 | ||
492 | ret = xencons_connect_backend(dev, info); | |
493 | if (ret < 0) | |
494 | goto error; | |
c0dccad8 | 495 | spin_lock_irqsave(&xencons_lock, flags); |
02e19f9c | 496 | list_add_tail(&info->list, &xenconsoles); |
c0dccad8 | 497 | spin_unlock_irqrestore(&xencons_lock, flags); |
02e19f9c SS |
498 | |
499 | return 0; | |
500 | ||
501 | error_nomem: | |
502 | ret = -ENOMEM; | |
503 | xenbus_dev_fatal(dev, ret, "allocating device memory"); | |
504 | error: | |
505 | xencons_disconnect_backend(info); | |
506 | xencons_free(info); | |
507 | return ret; | |
508 | } | |
509 | ||
510 | static int xencons_resume(struct xenbus_device *dev) | |
511 | { | |
512 | struct xencons_info *info = dev_get_drvdata(&dev->dev); | |
513 | ||
514 | xencons_disconnect_backend(info); | |
9652c080 | 515 | memset(info->intf, 0, XEN_PAGE_SIZE); |
02e19f9c | 516 | return xencons_connect_backend(dev, info); |
6b9b732d JF |
517 | } |
518 | ||
02e19f9c SS |
519 | static void xencons_backend_changed(struct xenbus_device *dev, |
520 | enum xenbus_state backend_state) | |
521 | { | |
522 | switch (backend_state) { | |
523 | case XenbusStateReconfiguring: | |
524 | case XenbusStateReconfigured: | |
525 | case XenbusStateInitialising: | |
526 | case XenbusStateInitialised: | |
527 | case XenbusStateUnknown: | |
02e19f9c SS |
528 | break; |
529 | ||
530 | case XenbusStateInitWait: | |
531 | break; | |
532 | ||
533 | case XenbusStateConnected: | |
534 | xenbus_switch_state(dev, XenbusStateConnected); | |
535 | break; | |
536 | ||
9b6934a3 DV |
537 | case XenbusStateClosed: |
538 | if (dev->state == XenbusStateClosed) | |
539 | break; | |
df561f66 | 540 | fallthrough; /* Missed the backend's CLOSING state */ |
02e19f9c SS |
541 | case XenbusStateClosing: |
542 | xenbus_frontend_closed(dev); | |
543 | break; | |
544 | } | |
545 | } | |
546 | ||
547 | static const struct xenbus_device_id xencons_ids[] = { | |
548 | { "console" }, | |
549 | { "" } | |
550 | }; | |
551 | ||
95afae48 DV |
552 | static struct xenbus_driver xencons_driver = { |
553 | .name = "xenconsole", | |
554 | .ids = xencons_ids, | |
cf8e019b SS |
555 | .probe = xencons_probe, |
556 | .remove = xencons_remove, | |
557 | .resume = xencons_resume, | |
558 | .otherend_changed = xencons_backend_changed, | |
02391434 | 559 | .not_essential = true, |
95afae48 | 560 | }; |
cf8e019b SS |
561 | #endif /* CONFIG_HVC_XEN_FRONTEND */ |
562 | ||
563 | static int __init xen_hvc_init(void) | |
564 | { | |
565 | int r; | |
566 | struct xencons_info *info; | |
567 | const struct hv_ops *ops; | |
568 | ||
569 | if (!xen_domain()) | |
570 | return -ENODEV; | |
571 | ||
572 | if (xen_initial_domain()) { | |
573 | ops = &dom0_hvc_ops; | |
574 | r = xen_initial_domain_console_init(); | |
575 | if (r < 0) | |
576 | return r; | |
577 | info = vtermno_to_xencons(HVC_COOKIE); | |
578 | } else { | |
579 | ops = &domU_hvc_ops; | |
580 | if (xen_hvm_domain()) | |
581 | r = xen_hvm_console_init(); | |
582 | else | |
583 | r = xen_pv_console_init(); | |
584 | if (r < 0) | |
585 | return r; | |
586 | ||
587 | info = vtermno_to_xencons(HVC_COOKIE); | |
fe415186 | 588 | info->irq = bind_evtchn_to_irq_lateeoi(info->evtchn); |
cf8e019b SS |
589 | } |
590 | if (info->irq < 0) | |
591 | info->irq = 0; /* NO_IRQ */ | |
592 | else | |
593 | irq_set_noprobe(info->irq); | |
594 | ||
595 | info->hvc = hvc_alloc(HVC_COOKIE, info->irq, ops, 256); | |
596 | if (IS_ERR(info->hvc)) { | |
c0dccad8 RPM |
597 | unsigned long flags; |
598 | ||
cf8e019b | 599 | r = PTR_ERR(info->hvc); |
c0dccad8 | 600 | spin_lock_irqsave(&xencons_lock, flags); |
cf8e019b | 601 | list_del(&info->list); |
c0dccad8 | 602 | spin_unlock_irqrestore(&xencons_lock, flags); |
cf8e019b SS |
603 | if (info->irq) |
604 | unbind_from_irqhandler(info->irq, NULL); | |
605 | kfree(info); | |
606 | return r; | |
607 | } | |
608 | ||
609 | r = 0; | |
610 | #ifdef CONFIG_HVC_XEN_FRONTEND | |
611 | r = xenbus_register_frontend(&xencons_driver); | |
612 | #endif | |
613 | return r; | |
614 | } | |
4fedd0bf | 615 | device_initcall(xen_hvc_init); |
b536b4b9 JF |
616 | |
617 | static int xen_cons_init(void) | |
618 | { | |
eb5ef071 | 619 | const struct hv_ops *ops; |
4fe7d5a7 | 620 | |
eb5ef071 | 621 | if (!xen_domain()) |
b536b4b9 JF |
622 | return 0; |
623 | ||
4fe7d5a7 JF |
624 | if (xen_initial_domain()) |
625 | ops = &dom0_hvc_ops; | |
eb5ef071 | 626 | else { |
02e19f9c | 627 | int r; |
4fe7d5a7 JF |
628 | ops = &domU_hvc_ops; |
629 | ||
02e19f9c SS |
630 | if (xen_hvm_domain()) |
631 | r = xen_hvm_console_init(); | |
eb5ef071 | 632 | else |
02e19f9c SS |
633 | r = xen_pv_console_init(); |
634 | if (r < 0) | |
635 | return r; | |
eb5ef071 SS |
636 | } |
637 | ||
4fe7d5a7 | 638 | hvc_instantiate(HVC_COOKIE, 0, ops); |
b536b4b9 JF |
639 | return 0; |
640 | } | |
b536b4b9 JF |
641 | console_initcall(xen_cons_init); |
642 | ||
a4d7b75b SS |
643 | #ifdef CONFIG_X86 |
644 | static void xen_hvm_early_write(uint32_t vtermno, const char *str, int len) | |
645 | { | |
646 | if (xen_cpuid_base()) | |
647 | outsb(0xe9, str, len); | |
648 | } | |
649 | #else | |
650 | static void xen_hvm_early_write(uint32_t vtermno, const char *str, int len) { } | |
651 | #endif | |
652 | ||
0922abdc | 653 | #ifdef CONFIG_EARLY_PRINTK |
6f2fdb29 | 654 | static int __init xenboot_console_setup(struct console *console, char *string) |
5de738b3 SS |
655 | { |
656 | static struct xencons_info xenboot; | |
657 | ||
42bc9716 | 658 | if (xen_initial_domain() || !xen_pv_domain()) |
5de738b3 | 659 | return 0; |
5de738b3 SS |
660 | |
661 | return xencons_info_pv_init(&xenboot, 0); | |
662 | } | |
663 | ||
b536b4b9 JF |
664 | static void xenboot_write_console(struct console *console, const char *string, |
665 | unsigned len) | |
666 | { | |
667 | unsigned int linelen, off = 0; | |
668 | const char *pos; | |
669 | ||
adf330a7 JB |
670 | if (dom0_write_console(0, string, len) >= 0) |
671 | return; | |
672 | ||
a4d7b75b SS |
673 | if (!xen_pv_domain()) { |
674 | xen_hvm_early_write(0, string, len); | |
eb5ef071 | 675 | return; |
a4d7b75b | 676 | } |
eb5ef071 | 677 | |
adf330a7 | 678 | if (domU_write_console(0, "(early) ", 8) < 0) |
4fe7d5a7 | 679 | return; |
b536b4b9 JF |
680 | while (off < len && NULL != (pos = strchr(string+off, '\n'))) { |
681 | linelen = pos-string+off; | |
682 | if (off + linelen > len) | |
683 | break; | |
4fe7d5a7 JF |
684 | domU_write_console(0, string+off, linelen); |
685 | domU_write_console(0, "\r\n", 2); | |
b536b4b9 JF |
686 | off += linelen + 1; |
687 | } | |
688 | if (off < len) | |
4fe7d5a7 | 689 | domU_write_console(0, string+off, len-off); |
b536b4b9 JF |
690 | } |
691 | ||
692 | struct console xenboot_console = { | |
693 | .name = "xenboot", | |
694 | .write = xenboot_write_console, | |
6f2fdb29 | 695 | .setup = xenboot_console_setup, |
0922abdc | 696 | .flags = CON_PRINTBUFFER | CON_BOOT | CON_ANYTIME, |
a9fbf4d5 | 697 | .index = -1, |
b536b4b9 | 698 | }; |
0922abdc | 699 | #endif /* CONFIG_EARLY_PRINTK */ |
0acf10d8 JF |
700 | |
701 | void xen_raw_console_write(const char *str) | |
702 | { | |
04b772d2 KRW |
703 | ssize_t len = strlen(str); |
704 | int rc = 0; | |
705 | ||
706 | if (xen_domain()) { | |
707 | rc = dom0_write_console(0, str, len); | |
a4d7b75b SS |
708 | if (rc != -ENOSYS || !xen_hvm_domain()) |
709 | return; | |
04b772d2 | 710 | } |
a4d7b75b | 711 | xen_hvm_early_write(0, str, len); |
0acf10d8 JF |
712 | } |
713 | ||
714 | void xen_raw_printk(const char *fmt, ...) | |
715 | { | |
716 | static char buf[512]; | |
717 | va_list ap; | |
718 | ||
719 | va_start(ap, fmt); | |
720 | vsnprintf(buf, sizeof(buf), fmt, ap); | |
721 | va_end(ap); | |
722 | ||
723 | xen_raw_console_write(buf); | |
724 | } | |
16e506ef SS |
725 | |
726 | static void xenboot_earlycon_write(struct console *console, | |
727 | const char *string, | |
728 | unsigned len) | |
729 | { | |
730 | dom0_write_console(0, string, len); | |
731 | } | |
732 | ||
733 | static int __init xenboot_earlycon_setup(struct earlycon_device *device, | |
734 | const char *opt) | |
735 | { | |
736 | device->con->write = xenboot_earlycon_write; | |
737 | return 0; | |
738 | } | |
739 | EARLYCON_DECLARE(xenboot, xenboot_earlycon_setup); |