Merge branch 'packaging'
[opensuse:kernel-source.git] / patches.xen / xen3-auto-common.diff
1 Subject: xen3 common
2 From: http://xenbits.xensource.com/linux-2.6.18-xen.hg (tip 1127:da0850ab55d6)
3 Patch-mainline: n/a
4 Acked-by: jbeulich@novell.com
5
6 List of files that don't require modification anymore (and hence got
7 removed from this patch), for reference and in case upstream wants to
8 take the forward porting patches:
9 2.6.19/include/linux/skbuff.h
10 2.6.19/net/core/dev.c
11 2.6.19/net/core/skbuff.c
12 2.6.19/net/ipv4/netfilter/nf_nat_proto_tcp.c
13 2.6.19/net/ipv4/netfilter/nf_nat_proto_udp.c
14 2.6.19/net/ipv4/xfrm4_output.c
15 2.6.22/include/linux/sched.h
16 2.6.22/kernel/softlockup.c
17 2.6.22/kernel/timer.c
18 2.6.25/mm/highmem.c
19 2.6.30/include/linux/pci_regs.h
20 2.6.35/kernel/time.c
21
22 --- head-2011-11-16.orig/drivers/Makefile       2011-11-16 16:26:18.000000000 +0100
23 +++ head-2011-11-16/drivers/Makefile    2011-11-16 17:01:23.000000000 +0100
24 @@ -47,6 +47,7 @@ obj-$(CONFIG_PARPORT)         += parport/
25  obj-y                          += base/ block/ misc/ mfd/ nfc/
26  obj-$(CONFIG_NUBUS)            += nubus/
27  obj-y                          += macintosh/
28 +obj-$(CONFIG_XEN)              += xen/
29  obj-$(CONFIG_IDE)              += ide/
30  obj-$(CONFIG_SCSI)             += scsi/
31  obj-$(CONFIG_ATA)              += ata/
32 --- head-2011-11-16.orig/drivers/acpi/Makefile  2011-07-22 04:17:23.000000000 +0200
33 +++ head-2011-11-16/drivers/acpi/Makefile       2011-04-13 11:25:29.000000000 +0200
34 @@ -67,6 +67,9 @@ obj-$(CONFIG_ACPI_CUSTOM_METHOD)+= custo
35  processor-y                    := processor_driver.o processor_throttling.o
36  processor-y                    += processor_idle.o processor_thermal.o
37  processor-$(CONFIG_CPU_FREQ)   += processor_perflib.o
38 +ifdef CONFIG_PROCESSOR_EXTERNAL_CONTROL
39 +processor-objs += processor_perflib.o processor_extcntl.o
40 +endif
41  
42  obj-$(CONFIG_ACPI_PROCESSOR_AGGREGATOR) += acpi_pad.o
43  obj-$(CONFIG_ACPI_IPMI)                += acpi_ipmi.o
44 --- head-2011-11-16.orig/drivers/acpi/acpica/hwsleep.c  2011-11-16 16:26:18.000000000 +0100
45 +++ head-2011-11-16/drivers/acpi/acpica/hwsleep.c       2011-11-16 17:01:18.000000000 +0100
46 @@ -237,7 +237,11 @@ acpi_status asmlinkage acpi_enter_sleep_
47         u32 pm1b_control;
48         struct acpi_bit_register_info *sleep_type_reg_info;
49         struct acpi_bit_register_info *sleep_enable_reg_info;
50 +#if !(defined(CONFIG_XEN) && defined(CONFIG_X86))
51         u32 in_value;
52 +#else
53 +       int err;
54 +#endif
55         struct acpi_object_list arg_list;
56         union acpi_object arg;
57         acpi_status status;
58 @@ -348,6 +352,7 @@ acpi_status asmlinkage acpi_enter_sleep_
59  
60         /* Write #2: Write both SLP_TYP + SLP_EN */
61  
62 +#if !(defined(CONFIG_XEN) && defined(CONFIG_X86))
63         status = acpi_hw_write_pm1_control(pm1a_control, pm1b_control);
64         if (ACPI_FAILURE(status)) {
65                 return_ACPI_STATUS(status);
66 @@ -387,6 +392,16 @@ acpi_status asmlinkage acpi_enter_sleep_
67                 /* Spin until we wake */
68  
69         } while (!in_value);
70 +#else
71 +       /* PV ACPI just need check hypercall return value */
72 +       err = acpi_notify_hypervisor_state(sleep_state,
73 +                       PM1Acontrol, PM1Bcontrol);
74 +       if (err) {
75 +               ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
76 +                                 "Hypervisor failure [%d]\n", err));
77 +               return_ACPI_STATUS(AE_ERROR);
78 +       }
79 +#endif
80  
81         return_ACPI_STATUS(AE_OK);
82  }
83 --- head-2011-11-16.orig/drivers/acpi/processor_driver.c        2011-11-16 16:26:18.000000000 +0100
84 +++ head-2011-11-16/drivers/acpi/processor_driver.c     2011-11-16 17:01:08.000000000 +0100
85 @@ -325,7 +325,8 @@ static int acpi_processor_get_info(struc
86          */
87         if (pr->id == -1) {
88                 if (ACPI_FAILURE
89 -                   (acpi_processor_hotadd_init(pr->handle, &pr->id))) {
90 +                   (acpi_processor_hotadd_init(pr->handle, &pr->id)) &&
91 +                   !processor_cntl_external()) {
92                         return -ENODEV;
93                 }
94         }
95 @@ -376,7 +377,11 @@ static int acpi_processor_get_info(struc
96         return 0;
97  }
98  
99 +#ifndef CONFIG_XEN
100  static DEFINE_PER_CPU(void *, processor_device_array);
101 +#else
102 +static void *processor_device_array[NR_ACPI_CPUS];
103 +#endif
104  
105  static void acpi_processor_notify(struct acpi_device *device, u32 event)
106  {
107 @@ -462,8 +467,11 @@ static int __cpuinit acpi_processor_add(
108         strcpy(acpi_device_class(device), ACPI_PROCESSOR_CLASS);
109         device->driver_data = pr;
110  
111 +       processor_extcntl_init();
112 +
113         result = acpi_processor_get_info(device);
114 -       if (result) {
115 +       if (result ||
116 +           ((pr->id == -1) && !processor_cntl_external())) {
117                 /* Processor is physically not present */
118                 return 0;
119         }
120 @@ -473,23 +481,36 @@ static int __cpuinit acpi_processor_add(
121                 return 0;
122  #endif
123  
124 -       BUG_ON((pr->id >= nr_cpu_ids) || (pr->id < 0));
125 +       BUG_ON(!processor_cntl_external() &&
126 +              ((pr->id >= nr_cpu_ids) || (pr->id < 0)));
127  
128         /*
129          * Buggy BIOS check
130          * ACPI id of processors can be reported wrongly by the BIOS.
131          * Don't trust it blindly
132          */
133 +#ifndef CONFIG_XEN
134         if (per_cpu(processor_device_array, pr->id) != NULL &&
135             per_cpu(processor_device_array, pr->id) != device) {
136 +#else
137 +       BUG_ON(pr->acpi_id >= NR_ACPI_CPUS);
138 +       if (processor_device_array[pr->acpi_id] != NULL &&
139 +           processor_device_array[pr->acpi_id] != device) {
140 +#endif
141                 printk(KERN_WARNING "BIOS reported wrong ACPI id "
142                         "for the processor\n");
143                 result = -ENODEV;
144                 goto err_free_cpumask;
145         }
146 +#ifndef CONFIG_XEN
147         per_cpu(processor_device_array, pr->id) = device;
148  
149         per_cpu(processors, pr->id) = pr;
150 +#else
151 +       processor_device_array[pr->acpi_id] = device;
152 +       if (pr->id != -1)
153 +               per_cpu(processors, pr->id) = pr;
154 +#endif
155  
156         sysdev = get_cpu_sysdev(pr->id);
157         if (sysfs_create_link(&device->dev.kobj, &sysdev->kobj, "sysdev")) {
158 @@ -497,15 +518,27 @@ static int __cpuinit acpi_processor_add(
159                 goto err_free_cpumask;
160         }
161  
162 -#ifdef CONFIG_CPU_FREQ
163 +#if defined(CONFIG_CPU_FREQ) || defined(CONFIG_PROCESSOR_EXTERNAL_CONTROL)
164         acpi_processor_ppc_has_changed(pr, 0);
165  #endif
166 -       acpi_processor_get_throttling_info(pr);
167 -       acpi_processor_get_limit_info(pr);
168 +       /*
169 +        * pr->id may equal to -1 while processor_cntl_external enabled.
170 +        * throttle and thermal module don't support this case.
171 +        * Tx only works when dom0 vcpu == pcpu num by far, as we give
172 +        * control to dom0.
173 +        */
174 +       if (pr->id != -1) {
175 +               acpi_processor_get_throttling_info(pr);
176 +               acpi_processor_get_limit_info(pr);
177 +       }
178  
179         if (!cpuidle_get_driver() || cpuidle_get_driver() == &acpi_idle_driver)
180                 acpi_processor_power_init(pr, device);
181  
182 +       result = processor_extcntl_prepare(pr);
183 +       if (result)
184 +               goto end;
185 +
186         pr->cdev = thermal_cooling_device_register("Processor", device,
187                                                 &processor_cooling_ops);
188         if (IS_ERR(pr->cdev)) {
189 @@ -555,7 +588,7 @@ static int acpi_processor_remove(struct 
190  
191         pr = acpi_driver_data(device);
192  
193 -       if (pr->id >= nr_cpu_ids)
194 +       if (!processor_cntl_external() && pr->id >= nr_cpu_ids)
195                 goto free;
196  
197         if (type == ACPI_BUS_REMOVAL_EJECT) {
198 @@ -574,8 +607,14 @@ static int acpi_processor_remove(struct 
199                 pr->cdev = NULL;
200         }
201  
202 +#ifndef CONFIG_XEN
203         per_cpu(processors, pr->id) = NULL;
204         per_cpu(processor_device_array, pr->id) = NULL;
205 +#else
206 +       if (pr->id != -1)
207 +               per_cpu(processors, pr->id) = NULL;
208 +       processor_device_array[pr->acpi_id] = NULL;
209 +#endif
210  
211  free:
212         free_cpumask_var(pr->throttling.shared_cpu_map);
213 @@ -631,6 +670,10 @@ int acpi_processor_device_add(acpi_handl
214                 return -ENODEV;
215         }
216  
217 +       if (processor_cntl_external() && acpi_driver_data(*device))
218 +               processor_notify_external(acpi_driver_data(*device),
219 +                       PROCESSOR_HOTPLUG, HOTPLUG_TYPE_ADD);
220 +
221         return 0;
222  }
223  
224 @@ -660,6 +703,10 @@ static void acpi_processor_hotplug_notif
225                                             "Unable to add the device\n");
226                         break;
227                 }
228 +               pr = acpi_driver_data(device);
229 +               if (processor_cntl_external() && pr)
230 +                       processor_notify_external(pr,
231 +                                       PROCESSOR_HOTPLUG, HOTPLUG_TYPE_ADD);
232                 break;
233         case ACPI_NOTIFY_EJECT_REQUEST:
234                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
235 @@ -676,6 +723,9 @@ static void acpi_processor_hotplug_notif
236                                     "Driver data is NULL, dropping EJECT\n");
237                         return;
238                 }
239 +               if (processor_cntl_external())
240 +                       processor_notify_external(pr, PROCESSOR_HOTPLUG,
241 +                                               HOTPLUG_TYPE_REMOVE);
242                 break;
243         default:
244                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
245 @@ -740,6 +790,11 @@ static acpi_status acpi_processor_hotadd
246  
247  static int acpi_processor_handle_eject(struct acpi_processor *pr)
248  {
249 +#ifdef CONFIG_XEN
250 +       if (pr->id == -1)
251 +               return (0);
252 +#endif
253 +
254         if (cpu_online(pr->id))
255                 cpu_down(pr->id);
256  
257 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
258 +++ head-2011-11-16/drivers/acpi/processor_extcntl.c    2011-01-31 14:53:38.000000000 +0100
259 @@ -0,0 +1,241 @@
260 +/*
261 + * processor_extcntl.c - channel to external control logic
262 + *
263 + *  Copyright (C) 2008, Intel corporation
264 + *
265 + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
266 + *
267 + *  This program is free software; you can redistribute it and/or modify
268 + *  it under the terms of the GNU General Public License as published by
269 + *  the Free Software Foundation; either version 2 of the License, or (at
270 + *  your option) any later version.
271 + *
272 + *  This program is distributed in the hope that it will be useful, but
273 + *  WITHOUT ANY WARRANTY; without even the implied warranty of
274 + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
275 + *  General Public License for more details.
276 + *
277 + *  You should have received a copy of the GNU General Public License along
278 + *  with this program; if not, write to the Free Software Foundation, Inc.,
279 + *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
280 + *
281 + */
282 +
283 +#include <linux/kernel.h>
284 +#include <linux/init.h>
285 +#include <linux/types.h>
286 +#include <linux/acpi.h>
287 +#include <linux/pm.h>
288 +#include <linux/cpu.h>
289 +
290 +#include <acpi/processor.h>
291 +
292 +#define ACPI_PROCESSOR_COMPONENT        0x01000000
293 +#define ACPI_PROCESSOR_CLASS            "processor"
294 +#define ACPI_PROCESSOR_DRIVER_NAME      "ACPI Processor Driver"
295 +#define _COMPONENT              ACPI_PROCESSOR_COMPONENT
296 +ACPI_MODULE_NAME("acpi_processor")
297 +
298 +static int processor_extcntl_parse_csd(struct acpi_processor *pr);
299 +static int processor_extcntl_get_performance(struct acpi_processor *pr);
300 +/*
301 + * External processor control logic may register with its own set of
302 + * ops to get ACPI related notification. One example is like VMM.
303 + */
304 +const struct processor_extcntl_ops *processor_extcntl_ops;
305 +EXPORT_SYMBOL(processor_extcntl_ops);
306 +
307 +static int processor_notify_smm(void)
308 +{
309 +       acpi_status status;
310 +       static int is_done = 0;
311 +
312 +       /* only need successfully notify BIOS once */
313 +       /* avoid double notification which may lead to unexpected result */
314 +       if (is_done)
315 +               return 0;
316 +
317 +       /* Can't write pstate_cnt to smi_cmd if either value is zero */
318 +       if ((!acpi_fadt.smi_cmd) || (!acpi_fadt.pstate_cnt)) {
319 +               ACPI_DEBUG_PRINT((ACPI_DB_INFO,"No SMI port or pstate_cnt\n"));
320 +               return 0;
321 +       }
322 +
323 +       ACPI_DEBUG_PRINT((ACPI_DB_INFO,
324 +               "Writing pstate_cnt [0x%x] to smi_cmd [0x%x]\n",
325 +               acpi_fadt.pstate_cnt, acpi_fadt.smi_cmd));
326 +
327 +       /* FADT v1 doesn't support pstate_cnt, many BIOS vendors use
328 +        * it anyway, so we need to support it... */
329 +       if (acpi_fadt_is_v1) {
330 +               ACPI_DEBUG_PRINT((ACPI_DB_INFO,
331 +                       "Using v1.0 FADT reserved value for pstate_cnt\n"));
332 +       }
333 +
334 +       status = acpi_os_write_port(acpi_fadt.smi_cmd,
335 +                                   (u32) acpi_fadt.pstate_cnt, 8);
336 +       if (ACPI_FAILURE(status))
337 +               return status;
338 +
339 +       is_done = 1;
340 +
341 +       return 0;
342 +}
343 +
344 +int processor_notify_external(struct acpi_processor *pr, int event, int type)
345 +{
346 +       int ret = -EINVAL;
347 +
348 +       if (!processor_cntl_external())
349 +               return -EINVAL;
350 +
351 +       switch (event) {
352 +       case PROCESSOR_PM_INIT:
353 +       case PROCESSOR_PM_CHANGE:
354 +               if ((type >= PM_TYPE_MAX) ||
355 +                       !processor_extcntl_ops->pm_ops[type])
356 +                       break;
357 +
358 +               ret = processor_extcntl_ops->pm_ops[type](pr, event);
359 +               break;
360 +       case PROCESSOR_HOTPLUG:
361 +               if (processor_extcntl_ops->hotplug)
362 +                       ret = processor_extcntl_ops->hotplug(pr, type);
363 +               break;
364 +       default:
365 +               printk(KERN_ERR "Unsupport processor events %d.\n", event);
366 +               break;
367 +       }
368 +
369 +       return ret;
370 +}
371 +
372 +/*
373 + * External control logic can decide to grab full or part of physical
374 + * processor control bits. Take a VMM for example, physical processors
375 + * are owned by VMM and thus existence information like hotplug is
376 + * always required to be notified to VMM. Similar is processor idle
377 + * state which is also necessarily controlled by VMM. But for other
378 + * control bits like performance/throttle states, VMM may choose to
379 + * control or not upon its own policy.
380 + */
381 +void processor_extcntl_init(void)
382 +{
383 +       if (!processor_extcntl_ops)
384 +               arch_acpi_processor_init_extcntl(&processor_extcntl_ops);
385 +}
386 +
387 +/*
388 + * This is called from ACPI processor init, and targeted to hold
389 + * some tricky housekeeping jobs to satisfy external control model.
390 + * For example, we may put dependency parse stub here for idle
391 + * and performance state. Those information may be not available
392 + * if splitting from dom0 control logic like cpufreq driver.
393 + */
394 +int processor_extcntl_prepare(struct acpi_processor *pr)
395 +{
396 +       /* parse cstate dependency information */
397 +       if (processor_pm_external())
398 +               processor_extcntl_parse_csd(pr);
399 +
400 +       /* Initialize performance states */
401 +       if (processor_pmperf_external())
402 +               processor_extcntl_get_performance(pr);
403 +
404 +       return 0;
405 +}
406 +
407 +/*
408 + * Currently no _CSD is implemented which is why existing ACPI code
409 + * doesn't parse _CSD at all. But to keep interface complete with
410 + * external control logic, we put a placeholder here for future
411 + * compatibility.
412 + */
413 +static int processor_extcntl_parse_csd(struct acpi_processor *pr)
414 +{
415 +       int i;
416 +
417 +       for (i = 0; i < pr->power.count; i++) {
418 +               if (!pr->power.states[i].valid)
419 +                       continue;
420 +
421 +               /* No dependency by default */
422 +               pr->power.states[i].domain_info = NULL;
423 +               pr->power.states[i].csd_count = 0;
424 +       }
425 +
426 +       return 0;
427 +}
428 +
429 +/*
430 + * Existing ACPI module does parse performance states at some point,
431 + * when acpi-cpufreq driver is loaded which however is something
432 + * we'd like to disable to avoid confliction with external control
433 + * logic. So we have to collect raw performance information here
434 + * when ACPI processor object is found and started.
435 + */
436 +static int processor_extcntl_get_performance(struct acpi_processor *pr)
437 +{
438 +       int ret;
439 +       struct acpi_processor_performance *perf;
440 +       struct acpi_psd_package *pdomain;
441 +
442 +       if (pr->performance)
443 +               return -EBUSY;
444 +
445 +       perf = kzalloc(sizeof(struct acpi_processor_performance), GFP_KERNEL);
446 +       if (!perf)
447 +               return -ENOMEM;
448 +
449 +       pr->performance = perf;
450 +       /* Get basic performance state information */
451 +       ret = acpi_processor_get_performance_info(pr);
452 +       if (ret < 0)
453 +               goto err_out;
454 +
455 +       /*
456 +        * Well, here we need retrieve performance dependency information
457 +        * from _PSD object. The reason why existing interface is not used
458 +        * is due to the reason that existing interface sticks to Linux cpu
459 +        * id to construct some bitmap, however we want to split ACPI
460 +        * processor objects from Linux cpu id logic. For example, even
461 +        * when Linux is configured as UP, we still want to parse all ACPI
462 +        * processor objects to external logic. In this case, it's preferred
463 +        * to use ACPI ID instead.
464 +        */
465 +       pdomain = &pr->performance->domain_info;
466 +       pdomain->num_processors = 0;
467 +       ret = acpi_processor_get_psd(pr);
468 +       if (ret < 0) {
469 +               /*
470 +                * _PSD is optional - assume no coordination if absent (or
471 +                * broken), matching native kernels' behavior.
472 +                */
473 +               pdomain->num_entries = ACPI_PSD_REV0_ENTRIES;
474 +               pdomain->revision = ACPI_PSD_REV0_REVISION;
475 +               pdomain->domain = pr->acpi_id;
476 +               pdomain->coord_type = DOMAIN_COORD_TYPE_SW_ALL;
477 +               pdomain->num_processors = 1;
478 +       }
479 +
480 +       /* Some sanity check */
481 +       if ((pdomain->revision != ACPI_PSD_REV0_REVISION) ||
482 +           (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) ||
483 +           ((pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL) &&
484 +            (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY) &&
485 +            (pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL))) {
486 +               ret = -EINVAL;
487 +               goto err_out;
488 +       }
489 +
490 +       /* Last step is to notify BIOS that external logic exists */
491 +       processor_notify_smm();
492 +
493 +       processor_notify_external(pr, PROCESSOR_PM_INIT, PM_TYPE_PERF);
494 +
495 +       return 0;
496 +err_out:
497 +       pr->performance = NULL;
498 +       kfree(perf);
499 +       return ret;
500 +}
501 --- head-2011-11-16.orig/drivers/acpi/processor_idle.c  2011-11-16 16:26:18.000000000 +0100
502 +++ head-2011-11-16/drivers/acpi/processor_idle.c       2011-11-16 17:01:10.000000000 +0100
503 @@ -449,7 +449,8 @@ static int acpi_processor_get_power_info
504                                  */
505                                 cx.entry_method = ACPI_CSTATE_HALT;
506                                 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
507 -                       } else {
508 +                       /* This doesn't apply to external control case */
509 +                       } else if (!processor_pm_external()) {
510                                 continue;
511                         }
512                         if (cx.type == ACPI_STATE_C1 &&
513 @@ -488,6 +489,12 @@ static int acpi_processor_get_power_info
514  
515                 cx.power = obj->integer.value;
516  
517 +#ifdef CONFIG_PROCESSOR_EXTERNAL_CONTROL
518 +               /* cache control methods to notify external logic */
519 +               if (processor_pm_external())
520 +                       memcpy(&cx.reg, reg, sizeof(*reg));
521 +#endif
522 +
523                 current_count++;
524                 memcpy(&(pr->power.states[current_count]), &cx, sizeof(cx));
525  
526 @@ -509,7 +516,7 @@ static int acpi_processor_get_power_info
527                           current_count));
528  
529         /* Validate number of power states discovered */
530 -       if (current_count < 2)
531 +       if (current_count < (processor_pm_external() ? 1 : 2))
532                 status = -EFAULT;
533  
534        end:
535 @@ -1259,6 +1266,11 @@ int __cpuinit acpi_processor_power_init(
536                 }
537                 acpi_processor_registered++;
538         }
539 +
540 +       if (processor_pm_external())
541 +               processor_notify_external(pr,
542 +                       PROCESSOR_PM_INIT, PM_TYPE_IDLE);
543 +
544         return 0;
545  }
546  
547 --- head-2011-11-16.orig/drivers/acpi/processor_perflib.c       2011-07-22 04:17:23.000000000 +0200
548 +++ head-2011-11-16/drivers/acpi/processor_perflib.c    2011-06-30 15:36:54.000000000 +0200
549 @@ -75,6 +75,7 @@ MODULE_PARM_DESC(ignore_ppc, "If the fre
550  
551  static int acpi_processor_ppc_status;
552  
553 +#ifdef CONFIG_CPU_FREQ
554  static int acpi_processor_ppc_notifier(struct notifier_block *nb,
555                                        unsigned long event, void *data)
556  {
557 @@ -117,6 +118,7 @@ static int acpi_processor_ppc_notifier(s
558  static struct notifier_block acpi_ppc_notifier_block = {
559         .notifier_call = acpi_processor_ppc_notifier,
560  };
561 +#endif /* CONFIG_CPU_FREQ */
562  
563  static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
564  {
565 @@ -205,7 +207,12 @@ int acpi_processor_ppc_has_changed(struc
566         if (ret < 0)
567                 return (ret);
568         else
569 +#ifdef CONFIG_CPU_FREQ
570                 return cpufreq_update_policy(pr->id);
571 +#elif defined(CONFIG_PROCESSOR_EXTERNAL_CONTROL)
572 +               return processor_notify_external(pr,
573 +                               PROCESSOR_PM_CHANGE, PM_TYPE_PERF);
574 +#endif
575  }
576  
577  int acpi_processor_get_bios_limit(int cpu, unsigned int *limit)
578 @@ -221,6 +228,7 @@ int acpi_processor_get_bios_limit(int cp
579  }
580  EXPORT_SYMBOL(acpi_processor_get_bios_limit);
581  
582 +#ifdef CONFIG_CPU_FREQ
583  void acpi_processor_ppc_init(void)
584  {
585         if (!cpufreq_register_notifier
586 @@ -239,6 +247,7 @@ void acpi_processor_ppc_exit(void)
587  
588         acpi_processor_ppc_status &= ~PPC_REGISTERED;
589  }
590 +#endif /* CONFIG_CPU_FREQ */
591  
592  static int acpi_processor_get_performance_control(struct acpi_processor *pr)
593  {
594 @@ -386,7 +395,10 @@ static int acpi_processor_get_performanc
595         return result;
596  }
597  
598 -static int acpi_processor_get_performance_info(struct acpi_processor *pr)
599 +#ifndef CONFIG_PROCESSOR_EXTERNAL_CONTROL
600 +static
601 +#endif
602 +int acpi_processor_get_performance_info(struct acpi_processor *pr)
603  {
604         int result = 0;
605         acpi_status status = AE_OK;
606 @@ -431,6 +443,7 @@ static int acpi_processor_get_performanc
607         return result;
608  }
609  
610 +#ifdef CONFIG_CPU_FREQ
611  int acpi_processor_notify_smm(struct module *calling_module)
612  {
613         acpi_status status;
614 @@ -491,8 +504,12 @@ int acpi_processor_notify_smm(struct mod
615  }
616  
617  EXPORT_SYMBOL(acpi_processor_notify_smm);
618 +#endif /* CONFIG_CPU_FREQ */
619  
620 -static int acpi_processor_get_psd(struct acpi_processor        *pr)
621 +#ifndef CONFIG_PROCESSOR_EXTERNAL_CONTROL
622 +static
623 +#endif
624 +int acpi_processor_get_psd(struct acpi_processor *pr)
625  {
626         int result = 0;
627         acpi_status status = AE_OK;
628 --- head-2011-11-16.orig/drivers/acpi/sleep.c   2011-11-16 16:26:18.000000000 +0100
629 +++ head-2011-11-16/drivers/acpi/sleep.c        2011-04-13 11:25:29.000000000 +0200
630 @@ -61,6 +61,7 @@ static struct notifier_block tts_notifie
631  static int acpi_sleep_prepare(u32 acpi_state)
632  {
633  #ifdef CONFIG_ACPI_SLEEP
634 +#ifndef CONFIG_ACPI_PV_SLEEP
635         /* do we have a wakeup address for S2 and S3? */
636         if (acpi_state == ACPI_STATE_S3) {
637                 if (!acpi_wakeup_address) {
638 @@ -70,6 +71,7 @@ static int acpi_sleep_prepare(u32 acpi_s
639                                 (acpi_physical_address)acpi_wakeup_address);
640  
641         }
642 +#endif
643         ACPI_FLUSH_CPU_CACHE();
644  #endif
645         printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n",
646 --- head-2011-11-16.orig/drivers/block/floppy.c 2011-10-24 09:10:05.000000000 +0200
647 +++ head-2011-11-16/drivers/block/floppy.c      2010-03-25 14:39:44.000000000 +0100
648 @@ -146,7 +146,9 @@
649  
650  #undef  FLOPPY_SILENT_DCL_CLEAR
651  
652 +#ifndef CONFIG_XEN
653  #define REALLY_SLOW_IO
654 +#endif
655  
656  #define DEBUGT 2
657  
658 --- head-2011-11-16.orig/drivers/char/agp/intel-gtt.c   2011-11-16 16:26:18.000000000 +0100
659 +++ head-2011-11-16/drivers/char/agp/intel-gtt.c        2011-04-13 11:25:29.000000000 +0200
660 @@ -147,8 +147,19 @@ static struct page *i8xx_alloc_pages(voi
661         if (page == NULL)
662                 return NULL;
663  
664 +#ifdef CONFIG_XEN
665 +       if (xen_create_contiguous_region((unsigned long)page_address(page), 2, 32)) {
666 +               __free_pages(page, 2);
667 +               return NULL;
668 +       }
669 +#endif
670 +
671         if (set_pages_uc(page, 4) < 0) {
672                 set_pages_wb(page, 4);
673 +#ifdef CONFIG_XEN
674 +               xen_destroy_contiguous_region((unsigned long)page_address(page),
675 +                                             2);
676 +#endif
677                 __free_pages(page, 2);
678                 return NULL;
679         }
680 @@ -163,6 +174,9 @@ static void i8xx_destroy_pages(struct pa
681                 return;
682  
683         set_pages_wb(page, 4);
684 +#ifdef CONFIG_XEN
685 +       xen_destroy_contiguous_region((unsigned long)page_address(page), 2);
686 +#endif
687         put_page(page);
688         __free_pages(page, 2);
689         atomic_dec(&agp_bridge->current_memory_agp);
690 --- head-2011-11-16.orig/drivers/char/mem.c     2011-11-16 16:26:18.000000000 +0100
691 +++ head-2011-11-16/drivers/char/mem.c  2011-11-16 17:01:34.000000000 +0100
692 @@ -87,6 +87,7 @@ void __weak unxlate_dev_mem_ptr(unsigned
693  {
694  }
695  
696 +#ifndef ARCH_HAS_DEV_MEM
697  /*
698   * This funcion reads the *physical* memory. The f_pos points directly to the
699   * memory location.
700 @@ -209,6 +210,7 @@ static ssize_t write_mem(struct file *fi
701         *ppos += written;
702         return written;
703  }
704 +#endif
705  
706  int __weak phys_mem_access_prot_allowed(struct file *file,
707         unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
708 @@ -335,6 +337,9 @@ static int mmap_mem(struct file *file, s
709  static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
710  {
711         unsigned long pfn;
712 +#ifdef CONFIG_XEN
713 +       unsigned long i, count;
714 +#endif
715  
716         /* Turn a kernel-virtual address into a physical page frame */
717         pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
718 @@ -349,6 +354,13 @@ static int mmap_kmem(struct file *file, 
719         if (!pfn_valid(pfn))
720                 return -EIO;
721  
722 +#ifdef CONFIG_XEN
723 +       count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
724 +       for (i = 0; i < count; i++)
725 +               if ((pfn + i) != mfn_to_local_pfn(pfn_to_mfn(pfn + i)))
726 +                       return -EIO;
727 +#endif
728 +
729         vma->vm_pgoff = pfn;
730         return mmap_mem(file, vma);
731  }
732 @@ -740,6 +752,7 @@ static int open_port(struct inode * inod
733  #define open_kmem      open_mem
734  #define open_oldmem    open_mem
735  
736 +#ifndef ARCH_HAS_DEV_MEM
737  static const struct file_operations mem_fops = {
738         .llseek         = memory_lseek,
739         .read           = read_mem,
740 @@ -748,6 +761,9 @@ static const struct file_operations mem_
741         .open           = open_mem,
742         .get_unmapped_area = get_unmapped_area_mem,
743  };
744 +#else
745 +extern const struct file_operations mem_fops;
746 +#endif
747  
748  #ifdef CONFIG_DEVKMEM
749  static const struct file_operations kmem_fops = {
750 --- head-2011-11-16.orig/drivers/char/tpm/Makefile      2006-09-20 05:42:06.000000000 +0200
751 +++ head-2011-11-16/drivers/char/tpm/Makefile   2011-04-13 11:25:29.000000000 +0200
752 @@ -9,3 +9,5 @@ obj-$(CONFIG_TCG_TIS) += tpm_tis.o
753  obj-$(CONFIG_TCG_NSC) += tpm_nsc.o
754  obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o
755  obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o
756 +obj-$(CONFIG_TCG_XEN) += tpm_xenu.o
757 +tpm_xenu-y = tpm_xen.o tpm_vtpm.o
758 --- head-2011-11-16.orig/drivers/char/tpm/tpm.h 2011-10-24 09:10:05.000000000 +0200
759 +++ head-2011-11-16/drivers/char/tpm/tpm.h      2011-09-07 14:38:52.000000000 +0200
760 @@ -115,6 +115,9 @@ struct tpm_chip {
761         struct dentry **bios_dir;
762  
763         struct list_head list;
764 +#ifdef CONFIG_XEN
765 +       void *priv;
766 +#endif
767         void (*release) (struct device *);
768  };
769  
770 @@ -279,6 +282,18 @@ struct tpm_cmd_t {
771  
772  ssize_t        tpm_getcap(struct device *, __be32, cap_t *, const char *);
773  
774 +#ifdef CONFIG_XEN
775 +static inline void *chip_get_private(const struct tpm_chip *chip)
776 +{
777 +       return chip->priv;
778 +}
779 +
780 +static inline void chip_set_private(struct tpm_chip *chip, void *priv)
781 +{
782 +       chip->priv = priv;
783 +}
784 +#endif
785 +
786  extern void tpm_get_timeouts(struct tpm_chip *);
787  extern void tpm_gen_interrupt(struct tpm_chip *);
788  extern void tpm_continue_selftest(struct tpm_chip *);
789 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
790 +++ head-2011-11-16/drivers/char/tpm/tpm_vtpm.c 2011-01-31 14:53:38.000000000 +0100
791 @@ -0,0 +1,542 @@
792 +/*
793 + * Copyright (C) 2006 IBM Corporation
794 + *
795 + * Authors:
796 + * Stefan Berger <stefanb@us.ibm.com>
797 + *
798 + * Generic device driver part for device drivers in a virtualized
799 + * environment.
800 + *
801 + * This program is free software; you can redistribute it and/or
802 + * modify it under the terms of the GNU General Public License as
803 + * published by the Free Software Foundation, version 2 of the
804 + * License.
805 + *
806 + */
807 +
808 +#include <asm/uaccess.h>
809 +#include <linux/list.h>
810 +#include <linux/device.h>
811 +#include <linux/interrupt.h>
812 +#include <linux/platform_device.h>
813 +#include "tpm.h"
814 +#include "tpm_vtpm.h"
815 +
816 +/* read status bits */
817 +enum {
818 +       STATUS_BUSY = 0x01,
819 +       STATUS_DATA_AVAIL = 0x02,
820 +       STATUS_READY = 0x04
821 +};
822 +
823 +struct transmission {
824 +       struct list_head next;
825 +
826 +       unsigned char *request;
827 +       size_t  request_len;
828 +       size_t  request_buflen;
829 +
830 +       unsigned char *response;
831 +       size_t  response_len;
832 +       size_t  response_buflen;
833 +
834 +       unsigned int flags;
835 +};
836 +
837 +enum {
838 +       TRANSMISSION_FLAG_WAS_QUEUED = 0x1
839 +};
840 +
841 +
842 +enum {
843 +       DATAEX_FLAG_QUEUED_ONLY = 0x1
844 +};
845 +
846 +
847 +/* local variables */
848 +
849 +/* local function prototypes */
850 +static int _vtpm_send_queued(struct tpm_chip *chip);
851 +
852 +
853 +/* =============================================================
854 + * Some utility functions
855 + * =============================================================
856 + */
857 +static void vtpm_state_init(struct vtpm_state *vtpms)
858 +{
859 +       vtpms->current_request = NULL;
860 +       spin_lock_init(&vtpms->req_list_lock);
861 +       init_waitqueue_head(&vtpms->req_wait_queue);
862 +       INIT_LIST_HEAD(&vtpms->queued_requests);
863 +
864 +       vtpms->current_response = NULL;
865 +       spin_lock_init(&vtpms->resp_list_lock);
866 +       init_waitqueue_head(&vtpms->resp_wait_queue);
867 +
868 +       vtpms->disconnect_time = jiffies;
869 +}
870 +
871 +
872 +static inline struct transmission *transmission_alloc(void)
873 +{
874 +       return kzalloc(sizeof(struct transmission), GFP_ATOMIC);
875 +}
876 +
877 +static unsigned char *
878 +transmission_set_req_buffer(struct transmission *t,
879 +                            unsigned char *buffer, size_t len)
880 +{
881 +       if (t->request_buflen < len) {
882 +               kfree(t->request);
883 +               t->request = kmalloc(len, GFP_KERNEL);
884 +               if (!t->request) {
885 +                       t->request_buflen = 0;
886 +                       return NULL;
887 +               }
888 +               t->request_buflen = len;
889 +       }
890 +
891 +       memcpy(t->request, buffer, len);
892 +       t->request_len = len;
893 +
894 +       return t->request;
895 +}
896 +
897 +static unsigned char *
898 +transmission_set_res_buffer(struct transmission *t,
899 +                            const unsigned char *buffer, size_t len)
900 +{
901 +       if (t->response_buflen < len) {
902 +               kfree(t->response);
903 +               t->response = kmalloc(len, GFP_ATOMIC);
904 +               if (!t->response) {
905 +                       t->response_buflen = 0;
906 +                       return NULL;
907 +               }
908 +               t->response_buflen = len;
909 +       }
910 +
911 +       memcpy(t->response, buffer, len);
912 +       t->response_len = len;
913 +
914 +       return t->response;
915 +}
916 +
917 +static inline void transmission_free(struct transmission *t)
918 +{
919 +       kfree(t->request);
920 +       kfree(t->response);
921 +       kfree(t);
922 +}
923 +
924 +/* =============================================================
925 + * Interface with the lower layer driver
926 + * =============================================================
927 + */
928 +/*
929 + * Lower layer uses this function to make a response available.
930 + */
931 +int vtpm_vd_recv(const struct tpm_chip *chip,
932 +                 const unsigned char *buffer, size_t count,
933 +                 void *ptr)
934 +{
935 +       unsigned long flags;
936 +       int ret_size = 0;
937 +       struct transmission *t;
938 +       struct vtpm_state *vtpms;
939 +
940 +       vtpms = (struct vtpm_state *)chip_get_private(chip);
941 +
942 +       /*
943 +        * The list with requests must contain one request
944 +        * only and the element there must be the one that
945 +        * was passed to me from the front-end.
946 +        */
947 +       spin_lock_irqsave(&vtpms->resp_list_lock, flags);
948 +       if (vtpms->current_request != ptr) {
949 +               spin_unlock_irqrestore(&vtpms->resp_list_lock, flags);
950 +               return 0;
951 +       }
952 +
953 +       if ((t = vtpms->current_request)) {
954 +               transmission_free(t);
955 +               vtpms->current_request = NULL;
956 +       }
957 +
958 +       t = transmission_alloc();
959 +       if (t) {
960 +               if (!transmission_set_res_buffer(t, buffer, count)) {
961 +                       transmission_free(t);
962 +                       spin_unlock_irqrestore(&vtpms->resp_list_lock, flags);
963 +                       return -ENOMEM;
964 +               }
965 +               ret_size = count;
966 +               vtpms->current_response = t;
967 +               wake_up_interruptible(&vtpms->resp_wait_queue);
968 +       }
969 +       spin_unlock_irqrestore(&vtpms->resp_list_lock, flags);
970 +
971 +       return ret_size;
972 +}
973 +
974 +
975 +/*
976 + * Lower layer indicates its status (connected/disconnected)
977 + */
978 +void vtpm_vd_status(const struct tpm_chip *chip, u8 vd_status)
979 +{
980 +       struct vtpm_state *vtpms;
981 +
982 +       vtpms = (struct vtpm_state *)chip_get_private(chip);
983 +
984 +       vtpms->vd_status = vd_status;
985 +       if ((vtpms->vd_status & TPM_VD_STATUS_CONNECTED) == 0) {
986 +               vtpms->disconnect_time = jiffies;
987 +       }
988 +}
989 +
990 +/* =============================================================
991 + * Interface with the generic TPM driver
992 + * =============================================================
993 + */
994 +static int vtpm_recv(struct tpm_chip *chip, u8 *buf, size_t count)
995 +{
996 +       int rc = 0;
997 +       unsigned long flags;
998 +       struct vtpm_state *vtpms;
999 +
1000 +       vtpms = (struct vtpm_state *)chip_get_private(chip);
1001 +
1002 +       /*
1003 +        * Check if the previous operation only queued the command
1004 +        * In this case there won't be a response, so I just
1005 +        * return from here and reset that flag. In any other
1006 +        * case I should receive a response from the back-end.
1007 +        */
1008 +       spin_lock_irqsave(&vtpms->resp_list_lock, flags);
1009 +       if ((vtpms->flags & DATAEX_FLAG_QUEUED_ONLY) != 0) {
1010 +               vtpms->flags &= ~DATAEX_FLAG_QUEUED_ONLY;
1011 +               spin_unlock_irqrestore(&vtpms->resp_list_lock, flags);
1012 +               /*
1013 +                * The first few commands (measurements) must be
1014 +                * queued since it might not be possible to talk to the
1015 +                * TPM, yet.
1016 +                * Return a response of up to 30 '0's.
1017 +                */
1018 +
1019 +               count = min_t(size_t, count, 30);
1020 +               memset(buf, 0x0, count);
1021 +               return count;
1022 +       }
1023 +       /*
1024 +        * Check whether something is in the responselist and if
1025 +        * there's nothing in the list wait for something to appear.
1026 +        */
1027 +
1028 +       if (!vtpms->current_response) {
1029 +               spin_unlock_irqrestore(&vtpms->resp_list_lock, flags);
1030 +               interruptible_sleep_on_timeout(&vtpms->resp_wait_queue,
1031 +                                              1000);
1032 +               spin_lock_irqsave(&vtpms->resp_list_lock ,flags);
1033 +       }
1034 +
1035 +       if (vtpms->current_response) {
1036 +               struct transmission *t = vtpms->current_response;
1037 +               vtpms->current_response = NULL;
1038 +               rc = min(count, t->response_len);
1039 +               memcpy(buf, t->response, rc);
1040 +               transmission_free(t);
1041 +       }
1042 +
1043 +       spin_unlock_irqrestore(&vtpms->resp_list_lock, flags);
1044 +       return rc;
1045 +}
1046 +
1047 +static int vtpm_send(struct tpm_chip *chip, u8 *buf, size_t count)
1048 +{
1049 +       int rc = 0;
1050 +       unsigned long flags;
1051 +       struct transmission *t = transmission_alloc();
1052 +       struct vtpm_state *vtpms;
1053 +
1054 +       vtpms = (struct vtpm_state *)chip_get_private(chip);
1055 +
1056 +       if (!t)
1057 +               return -ENOMEM;
1058 +       /*
1059 +        * If there's a current request, it must be the
1060 +        * previous request that has timed out.
1061 +        */
1062 +       spin_lock_irqsave(&vtpms->req_list_lock, flags);
1063 +       if (vtpms->current_request != NULL) {
1064 +               printk("WARNING: Sending although there is a request outstanding.\n"
1065 +                      "         Previous request must have timed out.\n");
1066 +               transmission_free(vtpms->current_request);
1067 +               vtpms->current_request = NULL;
1068 +       }
1069 +       spin_unlock_irqrestore(&vtpms->req_list_lock, flags);
1070 +
1071 +       /*
1072 +        * Queue the packet if the driver below is not
1073 +        * ready, yet, or there is any packet already
1074 +        * in the queue.
1075 +        * If the driver below is ready, unqueue all
1076 +        * packets first before sending our current
1077 +        * packet.
1078 +        * For each unqueued packet, except for the
1079 +        * last (=current) packet, call the function
1080 +        * tpm_xen_recv to wait for the response to come
1081 +        * back.
1082 +        */
1083 +       if ((vtpms->vd_status & TPM_VD_STATUS_CONNECTED) == 0) {
1084 +               if (time_after(jiffies,
1085 +                              vtpms->disconnect_time + HZ * 10)) {
1086 +                       rc = -ENOENT;
1087 +               } else {
1088 +                       goto queue_it;
1089 +               }
1090 +       } else {
1091 +               /*
1092 +                * Send all queued packets.
1093 +                */
1094 +               if (_vtpm_send_queued(chip) == 0) {
1095 +
1096 +                       vtpms->current_request = t;
1097 +
1098 +                       rc = vtpm_vd_send(vtpms->tpm_private,
1099 +                                         buf,
1100 +                                         count,
1101 +                                         t);
1102 +                       /*
1103 +                        * The generic TPM driver will call
1104 +                        * the function to receive the response.
1105 +                        */
1106 +                       if (rc < 0) {
1107 +                               vtpms->current_request = NULL;
1108 +                               goto queue_it;
1109 +                       }
1110 +               } else {
1111 +queue_it:
1112 +                       if (!transmission_set_req_buffer(t, buf, count)) {
1113 +                               transmission_free(t);
1114 +                               rc = -ENOMEM;
1115 +                               goto exit;
1116 +                       }
1117 +                       /*
1118 +                        * An error occurred. Don't event try
1119 +                        * to send the current request. Just
1120 +                        * queue it.
1121 +                        */
1122 +                       spin_lock_irqsave(&vtpms->req_list_lock, flags);
1123 +                       vtpms->flags |= DATAEX_FLAG_QUEUED_ONLY;
1124 +                       list_add_tail(&t->next, &vtpms->queued_requests);
1125 +                       spin_unlock_irqrestore(&vtpms->req_list_lock, flags);
1126 +               }
1127 +       }
1128 +
1129 +exit:
1130 +       return rc;
1131 +}
1132 +
1133 +
1134 +/*
1135 + * Send all queued requests.
1136 + */
1137 +static int _vtpm_send_queued(struct tpm_chip *chip)
1138 +{
1139 +       int rc;
1140 +       int error = 0;
1141 +       long flags;
1142 +       unsigned char buffer[1];
1143 +       struct vtpm_state *vtpms;
1144 +       vtpms = (struct vtpm_state *)chip_get_private(chip);
1145 +
1146 +       spin_lock_irqsave(&vtpms->req_list_lock, flags);
1147 +
1148 +       while (!list_empty(&vtpms->queued_requests)) {
1149 +               /*
1150 +                * Need to dequeue them.
1151 +                * Read the result into a dummy buffer.
1152 +                */
1153 +               struct transmission *qt = (struct transmission *)
1154 +                                         vtpms->queued_requests.next;
1155 +               list_del(&qt->next);
1156 +               vtpms->current_request = qt;
1157 +               spin_unlock_irqrestore(&vtpms->req_list_lock, flags);
1158 +
1159 +               rc = vtpm_vd_send(vtpms->tpm_private,
1160 +                                 qt->request,
1161 +                                 qt->request_len,
1162 +                                 qt);
1163 +
1164 +               if (rc < 0) {
1165 +                       spin_lock_irqsave(&vtpms->req_list_lock, flags);
1166 +                       if ((qt = vtpms->current_request) != NULL) {
1167 +                               /*
1168 +                                * requeue it at the beginning
1169 +                                * of the list
1170 +                                */
1171 +                               list_add(&qt->next,
1172 +                                        &vtpms->queued_requests);
1173 +                       }
1174 +                       vtpms->current_request = NULL;
1175 +                       error = 1;
1176 +                       break;
1177 +               }
1178 +               /*
1179 +                * After this point qt is not valid anymore!
1180 +                * It is freed when the front-end is delivering
1181 +                * the data by calling tpm_recv
1182 +                */
1183 +               /*
1184 +                * Receive response into provided dummy buffer
1185 +                */
1186 +               rc = vtpm_recv(chip, buffer, sizeof(buffer));
1187 +               spin_lock_irqsave(&vtpms->req_list_lock, flags);
1188 +       }
1189 +
1190 +       spin_unlock_irqrestore(&vtpms->req_list_lock, flags);
1191 +
1192 +       return error;
1193 +}
1194 +
1195 +static void vtpm_cancel(struct tpm_chip *chip)
1196 +{
1197 +       unsigned long flags;
1198 +       struct vtpm_state *vtpms = (struct vtpm_state *)chip_get_private(chip);
1199 +
1200 +       spin_lock_irqsave(&vtpms->resp_list_lock,flags);
1201 +
1202 +       if (!vtpms->current_response && vtpms->current_request) {
1203 +               spin_unlock_irqrestore(&vtpms->resp_list_lock, flags);
1204 +               interruptible_sleep_on(&vtpms->resp_wait_queue);
1205 +               spin_lock_irqsave(&vtpms->resp_list_lock,flags);
1206 +       }
1207 +
1208 +       if (vtpms->current_response) {
1209 +               struct transmission *t = vtpms->current_response;
1210 +               vtpms->current_response = NULL;
1211 +               transmission_free(t);
1212 +       }
1213 +
1214 +       spin_unlock_irqrestore(&vtpms->resp_list_lock,flags);
1215 +}
1216 +
1217 +static u8 vtpm_status(struct tpm_chip *chip)
1218 +{
1219 +       u8 rc = 0;
1220 +       unsigned long flags;
1221 +       struct vtpm_state *vtpms;
1222 +
1223 +       vtpms = (struct vtpm_state *)chip_get_private(chip);
1224 +
1225 +       spin_lock_irqsave(&vtpms->resp_list_lock, flags);
1226 +       /*
1227 +        * Data are available if:
1228 +        *  - there's a current response
1229 +        *  - the last packet was queued only (this is fake, but necessary to
1230 +        *      get the generic TPM layer to call the receive function.)
1231 +        */
1232 +       if (vtpms->current_response ||
1233 +           0 != (vtpms->flags & DATAEX_FLAG_QUEUED_ONLY)) {
1234 +               rc = STATUS_DATA_AVAIL;
1235 +       } else if (!vtpms->current_response && !vtpms->current_request) {
1236 +               rc = STATUS_READY;
1237 +       }
1238 +
1239 +       spin_unlock_irqrestore(&vtpms->resp_list_lock, flags);
1240 +       return rc;
1241 +}
1242 +
1243 +static struct file_operations vtpm_ops = {
1244 +       .owner = THIS_MODULE,
1245 +       .llseek = no_llseek,
1246 +       .open = tpm_open,
1247 +       .read = tpm_read,
1248 +       .write = tpm_write,
1249 +       .release = tpm_release,
1250 +};
1251 +
1252 +static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
1253 +static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
1254 +static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
1255 +static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
1256 +static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
1257 +static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated,
1258 +                  NULL);
1259 +static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps, NULL);
1260 +static DEVICE_ATTR(cancel, S_IWUSR |S_IWGRP, NULL, tpm_store_cancel);
1261 +
1262 +static struct attribute *vtpm_attrs[] = {
1263 +       &dev_attr_pubek.attr,
1264 +       &dev_attr_pcrs.attr,
1265 +       &dev_attr_enabled.attr,
1266 +       &dev_attr_active.attr,
1267 +       &dev_attr_owned.attr,
1268 +       &dev_attr_temp_deactivated.attr,
1269 +       &dev_attr_caps.attr,
1270 +       &dev_attr_cancel.attr,
1271 +       NULL,
1272 +};
1273 +
1274 +static struct attribute_group vtpm_attr_grp = { .attrs = vtpm_attrs };
1275 +
1276 +#define TPM_LONG_TIMEOUT   (10 * 60 * HZ)
1277 +
1278 +static struct tpm_vendor_specific tpm_vtpm = {
1279 +       .recv = vtpm_recv,
1280 +       .send = vtpm_send,
1281 +       .cancel = vtpm_cancel,
1282 +       .status = vtpm_status,
1283 +       .req_complete_mask = STATUS_BUSY | STATUS_DATA_AVAIL,
1284 +       .req_complete_val  = STATUS_DATA_AVAIL,
1285 +       .req_canceled = STATUS_READY,
1286 +       .attr_group = &vtpm_attr_grp,
1287 +       .miscdev = {
1288 +               .fops = &vtpm_ops,
1289 +       },
1290 +       .duration = {
1291 +               TPM_LONG_TIMEOUT,
1292 +               TPM_LONG_TIMEOUT,
1293 +               TPM_LONG_TIMEOUT,
1294 +       },
1295 +};
1296 +
1297 +struct tpm_chip *init_vtpm(struct device *dev,
1298 +                           struct tpm_private *tp)
1299 +{
1300 +       long rc;
1301 +       struct tpm_chip *chip;
1302 +       struct vtpm_state *vtpms;
1303 +
1304 +       vtpms = kzalloc(sizeof(struct vtpm_state), GFP_KERNEL);
1305 +       if (!vtpms)
1306 +               return ERR_PTR(-ENOMEM);
1307 +
1308 +       vtpm_state_init(vtpms);
1309 +       vtpms->tpm_private = tp;
1310 +
1311 +       chip = tpm_register_hardware(dev, &tpm_vtpm);
1312 +       if (!chip) {
1313 +               rc = -ENODEV;
1314 +               goto err_free_mem;
1315 +       }
1316 +
1317 +       chip_set_private(chip, vtpms);
1318 +
1319 +       return chip;
1320 +
1321 +err_free_mem:
1322 +       kfree(vtpms);
1323 +
1324 +       return ERR_PTR(rc);
1325 +}
1326 +
1327 +void cleanup_vtpm(struct device *dev)
1328 +{
1329 +       struct tpm_chip *chip = dev_get_drvdata(dev);
1330 +       struct vtpm_state *vtpms = (struct vtpm_state*)chip_get_private(chip);
1331 +       tpm_remove_hardware(dev);
1332 +       kfree(vtpms);
1333 +}
1334 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
1335 +++ head-2011-11-16/drivers/char/tpm/tpm_vtpm.h 2011-01-31 14:53:38.000000000 +0100
1336 @@ -0,0 +1,55 @@
1337 +#ifndef TPM_VTPM_H
1338 +#define TPM_VTPM_H
1339 +
1340 +struct tpm_chip;
1341 +struct tpm_private;
1342 +
1343 +struct vtpm_state {
1344 +       struct transmission *current_request;
1345 +       spinlock_t           req_list_lock;
1346 +       wait_queue_head_t    req_wait_queue;
1347 +
1348 +       struct list_head     queued_requests;
1349 +
1350 +       struct transmission *current_response;
1351 +       spinlock_t           resp_list_lock;
1352 +       wait_queue_head_t    resp_wait_queue;     // processes waiting for responses
1353 +
1354 +       u8                   vd_status;
1355 +       u8                   flags;
1356 +
1357 +       unsigned long        disconnect_time;
1358 +
1359 +       /*
1360 +        * The following is a private structure of the underlying
1361 +        * driver. It is passed as parameter in the send function.
1362 +        */
1363 +       struct tpm_private *tpm_private;
1364 +};
1365 +
1366 +
1367 +enum vdev_status {
1368 +       TPM_VD_STATUS_DISCONNECTED = 0x0,
1369 +       TPM_VD_STATUS_CONNECTED = 0x1
1370 +};
1371 +
1372 +/* this function is called from tpm_vtpm.c */
1373 +int vtpm_vd_send(struct tpm_private * tp,
1374 +                 const u8 * buf, size_t count, void *ptr);
1375 +
1376 +/* these functions are offered by tpm_vtpm.c */
1377 +struct tpm_chip *init_vtpm(struct device *,
1378 +                           struct tpm_private *);
1379 +void cleanup_vtpm(struct device *);
1380 +int vtpm_vd_recv(const struct tpm_chip* chip,
1381 +                 const unsigned char *buffer, size_t count, void *ptr);
1382 +void vtpm_vd_status(const struct tpm_chip *, u8 status);
1383 +
1384 +static inline struct tpm_private *tpm_private_from_dev(struct device *dev)
1385 +{
1386 +       struct tpm_chip *chip = dev_get_drvdata(dev);
1387 +       struct vtpm_state *vtpms = chip_get_private(chip);
1388 +       return vtpms->tpm_private;
1389 +}
1390 +
1391 +#endif
1392 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
1393 +++ head-2011-11-16/drivers/char/tpm/tpm_xen.c  2011-01-31 14:53:38.000000000 +0100
1394 @@ -0,0 +1,722 @@
1395 +/*
1396 + * Copyright (c) 2005, IBM Corporation
1397 + *
1398 + * Author: Stefan Berger, stefanb@us.ibm.com
1399 + * Grant table support: Mahadevan Gomathisankaran
1400 + *
1401 + * This code has been derived from drivers/xen/netfront/netfront.c
1402 + *
1403 + * Copyright (c) 2002-2004, K A Fraser
1404 + *
1405 + * This program is free software; you can redistribute it and/or
1406 + * modify it under the terms of the GNU General Public License version 2
1407 + * as published by the Free Software Foundation; or, when distributed
1408 + * separately from the Linux kernel or incorporated into other
1409 + * software packages, subject to the following license:
1410 + *
1411 + * Permission is hereby granted, free of charge, to any person obtaining a copy
1412 + * of this source file (the "Software"), to deal in the Software without
1413 + * restriction, including without limitation the rights to use, copy, modify,
1414 + * merge, publish, distribute, sublicense, and/or sell copies of the Software,
1415 + * and to permit persons to whom the Software is furnished to do so, subject to
1416 + * the following conditions:
1417 + *
1418 + * The above copyright notice and this permission notice shall be included in
1419 + * all copies or substantial portions of the Software.
1420 + *
1421 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1422 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1423 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1424 + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1425 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
1426 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
1427 + * IN THE SOFTWARE.
1428 + */
1429 +
1430 +#include <linux/errno.h>
1431 +#include <linux/err.h>
1432 +#include <linux/interrupt.h>
1433 +#include <linux/mutex.h>
1434 +#include <asm/uaccess.h>
1435 +#include <xen/evtchn.h>
1436 +#include <xen/interface/grant_table.h>
1437 +#include <xen/interface/io/tpmif.h>
1438 +#include <xen/gnttab.h>
1439 +#include <xen/xenbus.h>
1440 +#include "tpm.h"
1441 +#include "tpm_vtpm.h"
1442 +
1443 +#undef DEBUG
1444 +
1445 +/* local structures */
1446 +struct tpm_private {
1447 +       struct tpm_chip *chip;
1448 +
1449 +       tpmif_tx_interface_t *tx;
1450 +       atomic_t refcnt;
1451 +       unsigned int irq;
1452 +       u8 is_connected;
1453 +       u8 is_suspended;
1454 +
1455 +       spinlock_t tx_lock;
1456 +
1457 +       struct tx_buffer *tx_buffers[TPMIF_TX_RING_SIZE];
1458 +
1459 +       atomic_t tx_busy;
1460 +       void *tx_remember;
1461 +
1462 +       domid_t backend_id;
1463 +       wait_queue_head_t wait_q;
1464 +
1465 +       struct xenbus_device *dev;
1466 +       int ring_ref;
1467 +};
1468 +
1469 +struct tx_buffer {
1470 +       unsigned int size;      // available space in data
1471 +       unsigned int len;       // used space in data
1472 +       unsigned char *data;    // pointer to a page
1473 +};
1474 +
1475 +
1476 +/* locally visible variables */
1477 +static grant_ref_t gref_head;
1478 +static struct tpm_private *my_priv;
1479 +
1480 +/* local function prototypes */
1481 +static irqreturn_t tpmif_int(int irq,
1482 +                             void *tpm_priv,
1483 +                             struct pt_regs *ptregs);
1484 +static void tpmif_rx_action(unsigned long unused);
1485 +static int tpmif_connect(struct xenbus_device *dev,
1486 +                         struct tpm_private *tp,
1487 +                         domid_t domid);
1488 +static DECLARE_TASKLET(tpmif_rx_tasklet, tpmif_rx_action, 0);
1489 +static int tpmif_allocate_tx_buffers(struct tpm_private *tp);
1490 +static void tpmif_free_tx_buffers(struct tpm_private *tp);
1491 +static void tpmif_set_connected_state(struct tpm_private *tp,
1492 +                                      u8 newstate);
1493 +static int tpm_xmit(struct tpm_private *tp,
1494 +                    const u8 * buf, size_t count, int userbuffer,
1495 +                    void *remember);
1496 +static void destroy_tpmring(struct tpm_private *tp);
1497 +void __exit tpmif_exit(void);
1498 +
1499 +#define DPRINTK(fmt, args...) \
1500 +    pr_debug("xen_tpm_fr (%s:%d) " fmt, __FUNCTION__, __LINE__, ##args)
1501 +#define IPRINTK(fmt, args...) \
1502 +    printk(KERN_INFO "xen_tpm_fr: " fmt, ##args)
1503 +#define WPRINTK(fmt, args...) \
1504 +    printk(KERN_WARNING "xen_tpm_fr: " fmt, ##args)
1505 +
1506 +#define GRANT_INVALID_REF      0
1507 +
1508 +
1509 +static inline int
1510 +tx_buffer_copy(struct tx_buffer *txb, const u8 *src, int len,
1511 +               int isuserbuffer)
1512 +{
1513 +       int copied = len;
1514 +
1515 +       if (len > txb->size)
1516 +               copied = txb->size;
1517 +       if (isuserbuffer) {
1518 +               if (copy_from_user(txb->data, src, copied))
1519 +                       return -EFAULT;
1520 +       } else {
1521 +               memcpy(txb->data, src, copied);
1522 +       }
1523 +       txb->len = len;
1524 +       return copied;
1525 +}
1526 +
1527 +static inline struct tx_buffer *tx_buffer_alloc(void)
1528 +{
1529 +       struct tx_buffer *txb;
1530 +
1531 +       txb = kzalloc(sizeof(struct tx_buffer), GFP_KERNEL);
1532 +       if (!txb)
1533 +               return NULL;
1534 +
1535 +       txb->len = 0;
1536 +       txb->size = PAGE_SIZE;
1537 +       txb->data = (unsigned char *)__get_free_page(GFP_KERNEL);
1538 +       if (txb->data == NULL) {
1539 +               kfree(txb);
1540 +               txb = NULL;
1541 +       }
1542 +
1543 +       return txb;
1544 +}
1545 +
1546 +
1547 +static inline void tx_buffer_free(struct tx_buffer *txb)
1548 +{
1549 +       if (txb) {
1550 +               free_page((long)txb->data);
1551 +               kfree(txb);
1552 +       }
1553 +}
1554 +
1555 +/**************************************************************
1556 + Utility function for the tpm_private structure
1557 +**************************************************************/
1558 +static void tpm_private_init(struct tpm_private *tp)
1559 +{
1560 +       spin_lock_init(&tp->tx_lock);
1561 +       init_waitqueue_head(&tp->wait_q);
1562 +       atomic_set(&tp->refcnt, 1);
1563 +}
1564 +
1565 +static void tpm_private_put(void)
1566 +{
1567 +       if (!atomic_dec_and_test(&my_priv->refcnt))
1568 +               return;
1569 +
1570 +       tpmif_free_tx_buffers(my_priv);
1571 +       kfree(my_priv);
1572 +       my_priv = NULL;
1573 +}
1574 +
1575 +static struct tpm_private *tpm_private_get(void)
1576 +{
1577 +       int err;
1578 +
1579 +       if (my_priv) {
1580 +               atomic_inc(&my_priv->refcnt);
1581 +               return my_priv;
1582 +       }
1583 +
1584 +       my_priv = kzalloc(sizeof(struct tpm_private), GFP_KERNEL);
1585 +       if (!my_priv)
1586 +               return NULL;
1587 +
1588 +       tpm_private_init(my_priv);
1589 +       err = tpmif_allocate_tx_buffers(my_priv);
1590 +       if (err < 0)
1591 +               tpm_private_put();
1592 +
1593 +       return my_priv;
1594 +}
1595 +
1596 +/**************************************************************
1597 +
1598 + The interface to let the tpm plugin register its callback
1599 + function and send data to another partition using this module
1600 +
1601 +**************************************************************/
1602 +
1603 +static DEFINE_MUTEX(suspend_lock);
1604 +/*
1605 + * Send data via this module by calling this function
1606 + */
1607 +int vtpm_vd_send(struct tpm_private *tp,
1608 +                 const u8 * buf, size_t count, void *ptr)
1609 +{
1610 +       int sent;
1611 +
1612 +       mutex_lock(&suspend_lock);
1613 +       sent = tpm_xmit(tp, buf, count, 0, ptr);
1614 +       mutex_unlock(&suspend_lock);
1615 +
1616 +       return sent;
1617 +}
1618 +
1619 +/**************************************************************
1620 + XENBUS support code
1621 +**************************************************************/
1622 +
1623 +static int setup_tpmring(struct xenbus_device *dev,
1624 +                         struct tpm_private *tp)
1625 +{
1626 +       tpmif_tx_interface_t *sring;
1627 +       int err;
1628 +
1629 +       tp->ring_ref = GRANT_INVALID_REF;
1630 +
1631 +       sring = (void *)__get_free_page(GFP_KERNEL);
1632 +       if (!sring) {
1633 +               xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
1634 +               return -ENOMEM;
1635 +       }
1636 +       tp->tx = sring;
1637 +
1638 +       err = xenbus_grant_ring(dev, virt_to_mfn(tp->tx));
1639 +       if (err < 0) {
1640 +               free_page((unsigned long)sring);
1641 +               tp->tx = NULL;
1642 +               xenbus_dev_fatal(dev, err, "allocating grant reference");
1643 +               goto fail;
1644 +       }
1645 +       tp->ring_ref = err;
1646 +
1647 +       err = tpmif_connect(dev, tp, dev->otherend_id);
1648 +       if (err)
1649 +               goto fail;
1650 +
1651 +       return 0;
1652 +fail:
1653 +       destroy_tpmring(tp);
1654 +       return err;
1655 +}
1656 +
1657 +
1658 +static void destroy_tpmring(struct tpm_private *tp)
1659 +{
1660 +       tpmif_set_connected_state(tp, 0);
1661 +
1662 +       if (tp->ring_ref != GRANT_INVALID_REF) {
1663 +               gnttab_end_foreign_access(tp->ring_ref, (unsigned long)tp->tx);
1664 +               tp->ring_ref = GRANT_INVALID_REF;
1665 +               tp->tx = NULL;
1666 +       }
1667 +
1668 +       if (tp->irq)
1669 +               unbind_from_irqhandler(tp->irq, tp);
1670 +
1671 +       tp->irq = 0;
1672 +}
1673 +
1674 +
1675 +static int talk_to_backend(struct xenbus_device *dev,
1676 +                           struct tpm_private *tp)
1677 +{
1678 +       const char *message = NULL;
1679 +       int err;
1680 +       struct xenbus_transaction xbt;
1681 +
1682 +       err = setup_tpmring(dev, tp);
1683 +       if (err) {
1684 +               xenbus_dev_fatal(dev, err, "setting up ring");
1685 +               goto out;
1686 +       }
1687 +
1688 +again:
1689 +       err = xenbus_transaction_start(&xbt);
1690 +       if (err) {
1691 +               xenbus_dev_fatal(dev, err, "starting transaction");
1692 +               goto destroy_tpmring;
1693 +       }
1694 +
1695 +       err = xenbus_printf(xbt, dev->nodename,
1696 +                           "ring-ref","%u", tp->ring_ref);
1697 +       if (err) {
1698 +               message = "writing ring-ref";
1699 +               goto abort_transaction;
1700 +       }
1701 +
1702 +       err = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
1703 +                           irq_to_evtchn_port(tp->irq));
1704 +       if (err) {
1705 +               message = "writing event-channel";
1706 +               goto abort_transaction;
1707 +       }
1708 +
1709 +       err = xenbus_transaction_end(xbt, 0);
1710 +       if (err == -EAGAIN)
1711 +               goto again;
1712 +       if (err) {
1713 +               xenbus_dev_fatal(dev, err, "completing transaction");
1714 +               goto destroy_tpmring;
1715 +       }
1716 +
1717 +       xenbus_switch_state(dev, XenbusStateConnected);
1718 +
1719 +       return 0;
1720 +
1721 +abort_transaction:
1722 +       xenbus_transaction_end(xbt, 1);
1723 +       if (message)
1724 +               xenbus_dev_error(dev, err, "%s", message);
1725 +destroy_tpmring:
1726 +       destroy_tpmring(tp);
1727 +out:
1728 +       return err;
1729 +}
1730 +
1731 +/**
1732 + * Callback received when the backend's state changes.
1733 + */
1734 +static void backend_changed(struct xenbus_device *dev,
1735 +                           enum xenbus_state backend_state)
1736 +{
1737 +       struct tpm_private *tp = tpm_private_from_dev(&dev->dev);
1738 +       DPRINTK("\n");
1739 +
1740 +       switch (backend_state) {
1741 +       case XenbusStateInitialising:
1742 +       case XenbusStateInitWait:
1743 +       case XenbusStateInitialised:
1744 +       case XenbusStateReconfiguring:
1745 +       case XenbusStateReconfigured:
1746 +       case XenbusStateUnknown:
1747 +               break;
1748 +
1749 +       case XenbusStateConnected:
1750 +               tpmif_set_connected_state(tp, 1);
1751 +               break;
1752 +
1753 +       case XenbusStateClosing:
1754 +               tpmif_set_connected_state(tp, 0);
1755 +               xenbus_frontend_closed(dev);
1756 +               break;
1757 +
1758 +       case XenbusStateClosed:
1759 +               tpmif_set_connected_state(tp, 0);
1760 +               if (tp->is_suspended == 0)
1761 +                       device_unregister(&dev->dev);
1762 +               xenbus_frontend_closed(dev);
1763 +               break;
1764 +       }
1765 +}
1766 +
1767 +static int tpmfront_probe(struct xenbus_device *dev,
1768 +                          const struct xenbus_device_id *id)
1769 +{
1770 +       int err;
1771 +       int handle;
1772 +       struct tpm_private *tp = tpm_private_get();
1773 +
1774 +       if (!tp)
1775 +               return -ENOMEM;
1776 +
1777 +       tp->chip = init_vtpm(&dev->dev, tp);
1778 +       if (IS_ERR(tp->chip))
1779 +               return PTR_ERR(tp->chip);
1780 +
1781 +       err = xenbus_scanf(XBT_NIL, dev->nodename,
1782 +                          "handle", "%i", &handle);
1783 +       if (XENBUS_EXIST_ERR(err))
1784 +               return err;
1785 +
1786 +       if (err < 0) {
1787 +               xenbus_dev_fatal(dev,err,"reading virtual-device");
1788 +               return err;
1789 +       }
1790 +
1791 +       tp->dev = dev;
1792 +
1793 +       err = talk_to_backend(dev, tp);
1794 +       if (err) {
1795 +               tpm_private_put();
1796 +               return err;
1797 +       }
1798 +
1799 +       return 0;
1800 +}
1801 +
1802 +
1803 +static int tpmfront_remove(struct xenbus_device *dev)
1804 +{
1805 +       struct tpm_private *tp = tpm_private_from_dev(&dev->dev);
1806 +       destroy_tpmring(tp);
1807 +       cleanup_vtpm(&dev->dev);
1808 +       return 0;
1809 +}
1810 +
1811 +static int tpmfront_suspend(struct xenbus_device *dev)
1812 +{
1813 +       struct tpm_private *tp = tpm_private_from_dev(&dev->dev);
1814 +       u32 ctr;
1815 +
1816 +       /* Take the lock, preventing any application from sending. */
1817 +       mutex_lock(&suspend_lock);
1818 +       tp->is_suspended = 1;
1819 +
1820 +       for (ctr = 0; atomic_read(&tp->tx_busy); ctr++) {
1821 +               if ((ctr % 10) == 0)
1822 +                       printk("TPM-FE [INFO]: Waiting for outstanding "
1823 +                              "request.\n");
1824 +               /* Wait for a request to be responded to. */
1825 +               interruptible_sleep_on_timeout(&tp->wait_q, 100);
1826 +       }
1827 +
1828 +       return 0;
1829 +}
1830 +
1831 +static int tpmfront_suspend_finish(struct tpm_private *tp)
1832 +{
1833 +       tp->is_suspended = 0;
1834 +       /* Allow applications to send again. */
1835 +       mutex_unlock(&suspend_lock);
1836 +       return 0;
1837 +}
1838 +
1839 +static int tpmfront_suspend_cancel(struct xenbus_device *dev)
1840 +{
1841 +       struct tpm_private *tp = tpm_private_from_dev(&dev->dev);
1842 +       return tpmfront_suspend_finish(tp);
1843 +}
1844 +
1845 +static int tpmfront_resume(struct xenbus_device *dev)
1846 +{
1847 +       struct tpm_private *tp = tpm_private_from_dev(&dev->dev);
1848 +       destroy_tpmring(tp);
1849 +       return talk_to_backend(dev, tp);
1850 +}
1851 +
1852 +static int tpmif_connect(struct xenbus_device *dev,
1853 +                         struct tpm_private *tp,
1854 +                         domid_t domid)
1855 +{
1856 +       int err;
1857 +
1858 +       tp->backend_id = domid;
1859 +
1860 +       err = bind_listening_port_to_irqhandler(
1861 +               domid, tpmif_int, SA_SAMPLE_RANDOM, "tpmif", tp);
1862 +       if (err <= 0) {
1863 +               WPRINTK("bind_listening_port_to_irqhandler failed "
1864 +                       "(err=%d)\n", err);
1865 +               return err;
1866 +       }
1867 +       tp->irq = err;
1868 +
1869 +       return 0;
1870 +}
1871 +
1872 +static struct xenbus_device_id tpmfront_ids[] = {
1873 +       { "vtpm" },
1874 +       { "" }
1875 +};
1876 +
1877 +static struct xenbus_driver tpmfront = {
1878 +       .name = "vtpm",
1879 +       .owner = THIS_MODULE,
1880 +       .ids = tpmfront_ids,
1881 +       .probe = tpmfront_probe,
1882 +       .remove =  tpmfront_remove,
1883 +       .resume = tpmfront_resume,
1884 +       .otherend_changed = backend_changed,
1885 +       .suspend = tpmfront_suspend,
1886 +       .suspend_cancel = tpmfront_suspend_cancel,
1887 +};
1888 +
1889 +static void __init init_tpm_xenbus(void)
1890 +{
1891 +       xenbus_register_frontend(&tpmfront);
1892 +}
1893 +
1894 +static int tpmif_allocate_tx_buffers(struct tpm_private *tp)
1895 +{
1896 +       unsigned int i;
1897 +
1898 +       for (i = 0; i < TPMIF_TX_RING_SIZE; i++) {
1899 +               tp->tx_buffers[i] = tx_buffer_alloc();
1900 +               if (!tp->tx_buffers[i]) {
1901 +                       tpmif_free_tx_buffers(tp);
1902 +                       return -ENOMEM;
1903 +               }
1904 +       }
1905 +       return 0;
1906 +}
1907 +
1908 +static void tpmif_free_tx_buffers(struct tpm_private *tp)
1909 +{
1910 +       unsigned int i;
1911 +
1912 +       for (i = 0; i < TPMIF_TX_RING_SIZE; i++)
1913 +               tx_buffer_free(tp->tx_buffers[i]);
1914 +}
1915 +
1916 +static void tpmif_rx_action(unsigned long priv)
1917 +{
1918 +       struct tpm_private *tp = (struct tpm_private *)priv;
1919 +       int i = 0;
1920 +       unsigned int received;
1921 +       unsigned int offset = 0;
1922 +       u8 *buffer;
1923 +       tpmif_tx_request_t *tx = &tp->tx->ring[i].req;
1924 +
1925 +       atomic_set(&tp->tx_busy, 0);
1926 +       wake_up_interruptible(&tp->wait_q);
1927 +
1928 +       received = tx->size;
1929 +
1930 +       buffer = kmalloc(received, GFP_ATOMIC);
1931 +       if (!buffer)
1932 +               return;
1933 +
1934 +       for (i = 0; i < TPMIF_TX_RING_SIZE && offset < received; i++) {
1935 +               struct tx_buffer *txb = tp->tx_buffers[i];
1936 +               tpmif_tx_request_t *tx;
1937 +               unsigned int tocopy;
1938 +
1939 +               tx = &tp->tx->ring[i].req;
1940 +               tocopy = tx->size;
1941 +               if (tocopy > PAGE_SIZE)
1942 +                       tocopy = PAGE_SIZE;
1943 +
1944 +               memcpy(&buffer[offset], txb->data, tocopy);
1945 +
1946 +               gnttab_release_grant_reference(&gref_head, tx->ref);
1947 +
1948 +               offset += tocopy;
1949 +       }
1950 +
1951 +       vtpm_vd_recv(tp->chip, buffer, received, tp->tx_remember);
1952 +       kfree(buffer);
1953 +}
1954 +
1955 +
1956 +static irqreturn_t tpmif_int(int irq, void *tpm_priv, struct pt_regs *ptregs)
1957 +{
1958 +       struct tpm_private *tp = tpm_priv;
1959 +       unsigned long flags;
1960 +
1961 +       spin_lock_irqsave(&tp->tx_lock, flags);
1962 +       tpmif_rx_tasklet.data = (unsigned long)tp;
1963 +       tasklet_schedule(&tpmif_rx_tasklet);
1964 +       spin_unlock_irqrestore(&tp->tx_lock, flags);
1965 +
1966 +       return IRQ_HANDLED;
1967 +}
1968 +
1969 +
1970 +static int tpm_xmit(struct tpm_private *tp,
1971 +                    const u8 * buf, size_t count, int isuserbuffer,
1972 +                    void *remember)
1973 +{
1974 +       tpmif_tx_request_t *tx;
1975 +       TPMIF_RING_IDX i;
1976 +       unsigned int offset = 0;
1977 +
1978 +       spin_lock_irq(&tp->tx_lock);
1979 +
1980 +       if (unlikely(atomic_read(&tp->tx_busy))) {
1981 +               printk("tpm_xmit: There's an outstanding request/response "
1982 +                      "on the way!\n");
1983 +               spin_unlock_irq(&tp->tx_lock);
1984 +               return -EBUSY;
1985 +       }
1986 +
1987 +       if (tp->is_connected != 1) {
1988 +               spin_unlock_irq(&tp->tx_lock);
1989 +               return -EIO;
1990 +       }
1991 +
1992 +       for (i = 0; count > 0 && i < TPMIF_TX_RING_SIZE; i++) {
1993 +               struct tx_buffer *txb = tp->tx_buffers[i];
1994 +               int copied;
1995 +
1996 +               if (!txb) {
1997 +                       DPRINTK("txb (i=%d) is NULL. buffers initilized?\n"
1998 +                               "Not transmitting anything!\n", i);
1999 +                       spin_unlock_irq(&tp->tx_lock);
2000 +                       return -EFAULT;
2001 +               }
2002 +
2003 +               copied = tx_buffer_copy(txb, &buf[offset], count,
2004 +                                       isuserbuffer);
2005 +               if (copied < 0) {
2006 +                       /* An error occurred */
2007 +                       spin_unlock_irq(&tp->tx_lock);
2008 +                       return copied;
2009 +               }
2010 +               count -= copied;
2011 +               offset += copied;
2012 +
2013 +               tx = &tp->tx->ring[i].req;
2014 +               tx->addr = virt_to_machine(txb->data);
2015 +               tx->size = txb->len;
2016 +               tx->unused = 0;
2017 +
2018 +               DPRINTK("First 4 characters sent by TPM-FE are "
2019 +                       "0x%02x 0x%02x 0x%02x 0x%02x\n",
2020 +                       txb->data[0],txb->data[1],txb->data[2],txb->data[3]);
2021 +
2022 +               /* Get the granttable reference for this page. */
2023 +               tx->ref = gnttab_claim_grant_reference(&gref_head);
2024 +               if (tx->ref == -ENOSPC) {
2025 +                       spin_unlock_irq(&tp->tx_lock);
2026 +                       DPRINTK("Grant table claim reference failed in "
2027 +                               "func:%s line:%d file:%s\n",
2028 +                               __FUNCTION__, __LINE__, __FILE__);
2029 +                       return -ENOSPC;
2030 +               }
2031 +               gnttab_grant_foreign_access_ref(tx->ref,
2032 +                                               tp->backend_id,
2033 +                                               virt_to_mfn(txb->data),
2034 +                                               0 /*RW*/);
2035 +               wmb();
2036 +       }
2037 +
2038 +       atomic_set(&tp->tx_busy, 1);
2039 +       tp->tx_remember = remember;
2040 +
2041 +       mb();
2042 +
2043 +       notify_remote_via_irq(tp->irq);
2044 +
2045 +       spin_unlock_irq(&tp->tx_lock);
2046 +       return offset;
2047 +}
2048 +
2049 +
2050 +static void tpmif_notify_upperlayer(struct tpm_private *tp)
2051 +{
2052 +       /* Notify upper layer about the state of the connection to the BE. */
2053 +       vtpm_vd_status(tp->chip, (tp->is_connected
2054 +                                 ? TPM_VD_STATUS_CONNECTED
2055 +                                 : TPM_VD_STATUS_DISCONNECTED));
2056 +}
2057 +
2058 +
2059 +static void tpmif_set_connected_state(struct tpm_private *tp, u8 is_connected)
2060 +{
2061 +       /*
2062 +        * Don't notify upper layer if we are in suspend mode and
2063 +        * should disconnect - assumption is that we will resume
2064 +        * The mutex keeps apps from sending.
2065 +        */
2066 +       if (is_connected == 0 && tp->is_suspended == 1)
2067 +               return;
2068 +
2069 +       /*
2070 +        * Unlock the mutex if we are connected again
2071 +        * after being suspended - now resuming.
2072 +        * This also removes the suspend state.
2073 +        */
2074 +       if (is_connected == 1 && tp->is_suspended == 1)
2075 +               tpmfront_suspend_finish(tp);
2076 +
2077 +       if (is_connected != tp->is_connected) {
2078 +               tp->is_connected = is_connected;
2079 +               tpmif_notify_upperlayer(tp);
2080 +       }
2081 +}
2082 +
2083 +
2084 +
2085 +/* =================================================================
2086 + * Initialization function.
2087 + * =================================================================
2088 + */
2089 +
2090 +
2091 +static int __init tpmif_init(void)
2092 +{
2093 +       struct tpm_private *tp;
2094 +
2095 +       if (is_initial_xendomain())
2096 +               return -EPERM;
2097 +
2098 +       tp = tpm_private_get();
2099 +       if (!tp)
2100 +               return -ENOMEM;
2101 +
2102 +       IPRINTK("Initialising the vTPM driver.\n");
2103 +       if (gnttab_alloc_grant_references(TPMIF_TX_RING_SIZE,
2104 +                                         &gref_head) < 0) {
2105 +               tpm_private_put();
2106 +               return -EFAULT;
2107 +       }
2108 +
2109 +       init_tpm_xenbus();
2110 +       return 0;
2111 +}
2112 +
2113 +
2114 +module_init(tpmif_init);
2115 +
2116 +MODULE_LICENSE("Dual BSD/GPL");
2117 --- head-2011-11-16.orig/drivers/edac/edac_mc.c 2011-07-22 04:17:23.000000000 +0200
2118 +++ head-2011-11-16/drivers/edac/edac_mc.c      2011-06-30 15:37:01.000000000 +0200
2119 @@ -611,6 +611,10 @@ static void edac_mc_scrub_block(unsigned
2120  
2121         debugf3("%s()\n", __func__);
2122  
2123 +#ifdef CONFIG_XEN
2124 +       page = mfn_to_local_pfn(page);
2125 +#endif
2126 +
2127         /* ECC error page was not in our memory. Ignore it. */
2128         if (!pfn_valid(page))
2129                 return;
2130 --- head-2011-11-16.orig/drivers/firmware/dell_rbu.c    2010-08-02 00:11:14.000000000 +0200
2131 +++ head-2011-11-16/drivers/firmware/dell_rbu.c 2011-04-13 11:25:29.000000000 +0200
2132 @@ -170,9 +170,28 @@ static int create_packet(void *data, siz
2133                         spin_lock(&rbu_data.lock);
2134                         goto out_alloc_packet_array;
2135                 }
2136 +#ifdef CONFIG_XEN
2137 +               if (ordernum && xen_create_contiguous_region(
2138 +                       (unsigned long)packet_data_temp_buf, ordernum, 0)) {
2139 +                       free_pages((unsigned long)packet_data_temp_buf,
2140 +                                  ordernum);
2141 +                       printk(KERN_WARNING
2142 +                               "dell_rbu:%s: failed to adjust new "
2143 +                               "packet\n", __func__);
2144 +                       retval = -ENOMEM;
2145 +                       spin_lock(&rbu_data.lock);
2146 +                       goto out_alloc_packet_array;
2147 +               }
2148 +#endif
2149  
2150 -               if ((unsigned long)virt_to_phys(packet_data_temp_buf)
2151 +               if ((unsigned long)virt_to_bus(packet_data_temp_buf)
2152                                 < allocation_floor) {
2153 +#ifdef CONFIG_XEN
2154 +                       if (ordernum)
2155 +                               xen_destroy_contiguous_region(
2156 +                                       (unsigned long)packet_data_temp_buf,
2157 +                                       ordernum);
2158 +#endif
2159                         pr_debug("packet 0x%lx below floor at 0x%lx.\n",
2160                                         (unsigned long)virt_to_phys(
2161                                                 packet_data_temp_buf),
2162 @@ -186,7 +205,7 @@ static int create_packet(void *data, siz
2163         newpacket->data = packet_data_temp_buf;
2164  
2165         pr_debug("create_packet: newpacket at physical addr %lx\n",
2166 -               (unsigned long)virt_to_phys(newpacket->data));
2167 +               (unsigned long)virt_to_bus(newpacket->data));
2168  
2169         /* packets may not have fixed size */
2170         newpacket->length = length;
2171 @@ -205,7 +224,7 @@ out_alloc_packet_array:
2172         /* always free packet array */
2173         for (;idx>0;idx--) {
2174                 pr_debug("freeing unused packet below floor 0x%lx.\n",
2175 -                       (unsigned long)virt_to_phys(
2176 +                       (unsigned long)virt_to_bus(
2177                                 invalid_addr_packet_array[idx-1]));
2178                 free_pages((unsigned long)invalid_addr_packet_array[idx-1],
2179                         ordernum);
2180 @@ -349,6 +368,13 @@ static void packet_empty_list(void)
2181                  * to make sure there are no stale RBU packets left in memory
2182                  */
2183                 memset(newpacket->data, 0, rbu_data.packetsize);
2184 +#ifdef CONFIG_XEN
2185 +               if (newpacket->ordernum)
2186 +                       xen_destroy_contiguous_region(
2187 +                               (unsigned long)newpacket->data,
2188 +                               newpacket->ordernum);
2189 +#endif
2190 +
2191                 free_pages((unsigned long) newpacket->data,
2192                         newpacket->ordernum);
2193                 kfree(newpacket);
2194 @@ -403,7 +429,9 @@ static int img_update_realloc(unsigned l
2195  {
2196         unsigned char *image_update_buffer = NULL;
2197         unsigned long rc;
2198 +#ifndef CONFIG_XEN
2199         unsigned long img_buf_phys_addr;
2200 +#endif
2201         int ordernum;
2202         int dma_alloc = 0;
2203  
2204 @@ -434,15 +462,19 @@ static int img_update_realloc(unsigned l
2205  
2206         spin_unlock(&rbu_data.lock);
2207  
2208 +#ifndef CONFIG_XEN
2209         ordernum = get_order(size);
2210         image_update_buffer =
2211                 (unsigned char *) __get_free_pages(GFP_KERNEL, ordernum);
2212  
2213         img_buf_phys_addr =
2214 -               (unsigned long) virt_to_phys(image_update_buffer);
2215 +               (unsigned long) virt_to_bus(image_update_buffer);
2216  
2217         if (img_buf_phys_addr > BIOS_SCAN_LIMIT) {
2218                 free_pages((unsigned long) image_update_buffer, ordernum);
2219 +#else
2220 +       {
2221 +#endif
2222                 ordernum = -1;
2223                 image_update_buffer = dma_alloc_coherent(NULL, size,
2224                         &dell_rbu_dmaaddr, GFP_KERNEL);
2225 @@ -695,6 +727,12 @@ static struct bin_attribute rbu_packet_s
2226  static int __init dcdrbu_init(void)
2227  {
2228         int rc;
2229 +
2230 +#ifdef CONFIG_XEN
2231 +       if (!is_initial_xendomain())
2232 +               return -ENODEV;
2233 +#endif
2234 +
2235         spin_lock_init(&rbu_data.lock);
2236  
2237         init_packet_head();
2238 --- head-2011-11-16.orig/drivers/ide/ide-lib.c  2011-11-16 16:26:19.000000000 +0100
2239 +++ head-2011-11-16/drivers/ide/ide-lib.c       2011-11-16 17:01:48.000000000 +0100
2240 @@ -19,12 +19,12 @@ void ide_toggle_bounce(ide_drive_t *driv
2241  {
2242         u64 addr = BLK_BOUNCE_HIGH;     /* dma64_addr_t */
2243  
2244 -       if (!PCI_DMA_BUS_IS_PHYS) {
2245 -               addr = BLK_BOUNCE_ANY;
2246 -       } else if (on && drive->media == ide_disk) {
2247 +       if (on && drive->media == ide_disk) {
2248                 struct device *dev = drive->hwif->dev;
2249  
2250 -               if (dev && dev->dma_mask)
2251 +               if (!PCI_DMA_BUS_IS_PHYS)
2252 +                       addr = BLK_BOUNCE_ANY;
2253 +               else if (dev && dev->dma_mask)
2254                         addr = *dev->dma_mask;
2255         }
2256  
2257 --- head-2011-11-16.orig/drivers/oprofile/buffer_sync.c 2011-07-22 04:17:23.000000000 +0200
2258 +++ head-2011-11-16/drivers/oprofile/buffer_sync.c      2011-06-30 15:37:10.000000000 +0200
2259 @@ -8,6 +8,10 @@
2260   * @author Barry Kasindorf
2261   * @author Robert Richter <robert.richter@amd.com>
2262   *
2263 + * Modified by Aravind Menon for Xen
2264 + * These modifications are:
2265 + * Copyright (C) 2005 Hewlett-Packard Co.
2266 + *
2267   * This is the core of the buffer management. Each
2268   * CPU buffer is processed and entered into the
2269   * global event buffer. Such processing is necessary
2270 @@ -43,6 +47,8 @@ static cpumask_var_t marked_cpus;
2271  static DEFINE_SPINLOCK(task_mortuary);
2272  static void process_task_mortuary(void);
2273  
2274 +static int cpu_current_domain[NR_CPUS];
2275 +
2276  /* Take ownership of the task struct and place it on the
2277   * list for processing. Only after two full buffer syncs
2278   * does the task eventually get freed, because by then
2279 @@ -61,7 +67,6 @@ task_free_notify(struct notifier_block *
2280         return NOTIFY_OK;
2281  }
2282  
2283 -
2284  /* The task is on its way out. A sync of the buffer means we can catch
2285   * any remaining samples for this task.
2286   */
2287 @@ -151,6 +156,11 @@ static void free_all_tasks(void)
2288  int sync_start(void)
2289  {
2290         int err;
2291 +       int i;
2292 +
2293 +       for (i = 0; i < NR_CPUS; i++) {
2294 +               cpu_current_domain[i] = COORDINATOR_DOMAIN;
2295 +       }
2296  
2297         if (!zalloc_cpumask_var(&marked_cpus, GFP_KERNEL))
2298                 return -ENOMEM;
2299 @@ -287,13 +297,29 @@ static void add_cpu_switch(int i)
2300         last_cookie = INVALID_COOKIE;
2301  }
2302  
2303 -static void add_kernel_ctx_switch(unsigned int in_kernel)
2304 +static void add_cpu_mode_switch(unsigned int cpu_mode)
2305  {
2306         add_event_entry(ESCAPE_CODE);
2307 -       if (in_kernel)
2308 +       switch (cpu_mode) {
2309 +       case CPU_MODE_USER:
2310 +               add_event_entry(USER_ENTER_SWITCH_CODE);
2311 +               break;
2312 +       case CPU_MODE_KERNEL:
2313                 add_event_entry(KERNEL_ENTER_SWITCH_CODE);
2314 -       else
2315 -               add_event_entry(KERNEL_EXIT_SWITCH_CODE);
2316 +               break;
2317 +       case CPU_MODE_XEN:
2318 +               add_event_entry(XEN_ENTER_SWITCH_CODE);
2319 +               break;
2320 +       default:
2321 +               break;
2322 +       }
2323 +}
2324 +
2325 +static void add_domain_switch(unsigned long domain_id)
2326 +{
2327 +       add_event_entry(ESCAPE_CODE);
2328 +       add_event_entry(DOMAIN_SWITCH_CODE);
2329 +       add_event_entry(domain_id);
2330  }
2331  
2332  static void
2333 @@ -374,12 +400,12 @@ static inline void add_sample_entry(unsi
2334   * for later lookup from userspace. Return 0 on failure.
2335   */
2336  static int
2337 -add_sample(struct mm_struct *mm, struct op_sample *s, int in_kernel)
2338 +add_sample(struct mm_struct *mm, struct op_sample *s, int cpu_mode)
2339  {
2340         unsigned long cookie;
2341         off_t offset;
2342  
2343 -       if (in_kernel) {
2344 +       if (cpu_mode >= CPU_MODE_KERNEL) {
2345                 add_sample_entry(s->eip, s->event);
2346                 return 1;
2347         }
2348 @@ -504,9 +530,10 @@ void sync_buffer(int cpu)
2349         unsigned long val;
2350         struct task_struct *new;
2351         unsigned long cookie = 0;
2352 -       int in_kernel = 1;
2353 +       int cpu_mode = CPU_MODE_KERNEL;
2354         sync_buffer_state state = sb_buffer_start;
2355         unsigned int i;
2356 +       int domain_switch = 0;
2357         unsigned long available;
2358         unsigned long flags;
2359         struct op_entry entry;
2360 @@ -516,6 +543,11 @@ void sync_buffer(int cpu)
2361  
2362         add_cpu_switch(cpu);
2363  
2364 +       /* We need to assign the first samples in this CPU buffer to the
2365 +          same domain that we were processing at the last sync_buffer */
2366 +       if (cpu_current_domain[cpu] != COORDINATOR_DOMAIN)
2367 +               add_domain_switch(cpu_current_domain[cpu]);
2368 +
2369         op_cpu_buffer_reset(cpu);
2370         available = op_cpu_buffer_entries(cpu);
2371  
2372 @@ -524,6 +556,13 @@ void sync_buffer(int cpu)
2373                 if (!sample)
2374                         break;
2375  
2376 +               if (domain_switch) {
2377 +                       cpu_current_domain[cpu] = sample->eip;
2378 +                       add_domain_switch(sample->eip);
2379 +                       domain_switch = 0;
2380 +                       continue;
2381 +               }
2382 +
2383                 if (is_code(sample->eip)) {
2384                         flags = sample->event;
2385                         if (flags & TRACE_BEGIN) {
2386 @@ -532,10 +571,10 @@ void sync_buffer(int cpu)
2387                         }
2388                         if (flags & KERNEL_CTX_SWITCH) {
2389                                 /* kernel/userspace switch */
2390 -                               in_kernel = flags & IS_KERNEL;
2391 +                               cpu_mode = flags & CPU_MODE_MASK;
2392                                 if (state == sb_buffer_start)
2393                                         state = sb_sample_start;
2394 -                               add_kernel_ctx_switch(flags & IS_KERNEL);
2395 +                               add_cpu_mode_switch(cpu_mode);
2396                         }
2397                         if (flags & USER_CTX_SWITCH
2398                             && op_cpu_buffer_get_data(&entry, &val)) {
2399 @@ -548,16 +587,23 @@ void sync_buffer(int cpu)
2400                                         cookie = get_exec_dcookie(mm);
2401                                 add_user_ctx_switch(new, cookie);
2402                         }
2403 +                       if (flags & DOMAIN_SWITCH)
2404 +                               domain_switch = 1;
2405                         if (op_cpu_buffer_get_size(&entry))
2406                                 add_data(&entry, mm);
2407                         continue;
2408                 }
2409  
2410 +               if (cpu_current_domain[cpu] != COORDINATOR_DOMAIN) {
2411 +                       add_sample_entry(sample->eip, sample->event);
2412 +                       continue;
2413 +               }
2414 +
2415                 if (state < sb_bt_start)
2416                         /* ignore sample */
2417                         continue;
2418  
2419 -               if (add_sample(mm, sample, in_kernel))
2420 +               if (add_sample(mm, sample, cpu_mode))
2421                         continue;
2422  
2423                 /* ignore backtraces if failed to add a sample */
2424 @@ -568,6 +614,10 @@ void sync_buffer(int cpu)
2425         }
2426         release_mm(mm);
2427  
2428 +       /* We reset domain to COORDINATOR at each CPU switch */
2429 +       if (cpu_current_domain[cpu] != COORDINATOR_DOMAIN)
2430 +               add_domain_switch(COORDINATOR_DOMAIN);
2431 +
2432         mark_done(cpu);
2433  
2434         mutex_unlock(&buffer_mutex);
2435 --- head-2011-11-16.orig/drivers/oprofile/cpu_buffer.c  2011-05-19 06:06:34.000000000 +0200
2436 +++ head-2011-11-16/drivers/oprofile/cpu_buffer.c       2011-04-13 11:25:29.000000000 +0200
2437 @@ -8,6 +8,10 @@
2438   * @author Barry Kasindorf <barry.kasindorf@amd.com>
2439   * @author Robert Richter <robert.richter@amd.com>
2440   *
2441 + * Modified by Aravind Menon for Xen
2442 + * These modifications are:
2443 + * Copyright (C) 2005 Hewlett-Packard Co.
2444 + *
2445   * Each CPU has a local buffer that stores PC value/event
2446   * pairs. We also log context switches when we notice them.
2447   * Eventually each CPU's buffer is processed into the global
2448 @@ -38,6 +42,8 @@ static void wq_sync_buffer(struct work_s
2449  #define DEFAULT_TIMER_EXPIRE (HZ / 10)
2450  static int work_enabled;
2451  
2452 +static int32_t current_domain = COORDINATOR_DOMAIN;
2453 +
2454  unsigned long oprofile_get_cpu_buffer_size(void)
2455  {
2456         return oprofile_cpu_buffer_size;
2457 @@ -75,7 +81,7 @@ int alloc_cpu_buffers(void)
2458                 struct oprofile_cpu_buffer *b = &per_cpu(op_cpu_buffer, i);
2459  
2460                 b->last_task = NULL;
2461 -               b->last_is_kernel = -1;
2462 +               b->last_cpu_mode = -1;
2463                 b->tracing = 0;
2464                 b->buffer_size = buffer_size;
2465                 b->sample_received = 0;
2466 @@ -180,7 +186,7 @@ unsigned long op_cpu_buffer_entries(int 
2467  
2468  static int
2469  op_add_code(struct oprofile_cpu_buffer *cpu_buf, unsigned long backtrace,
2470 -           int is_kernel, struct task_struct *task)
2471 +           int cpu_mode, struct task_struct *task)
2472  {
2473         struct op_entry entry;
2474         struct op_sample *sample;
2475 @@ -193,16 +199,15 @@ op_add_code(struct oprofile_cpu_buffer *
2476                 flags |= TRACE_BEGIN;
2477  
2478         /* notice a switch from user->kernel or vice versa */
2479 -       is_kernel = !!is_kernel;
2480 -       if (cpu_buf->last_is_kernel != is_kernel) {
2481 -               cpu_buf->last_is_kernel = is_kernel;
2482 -               flags |= KERNEL_CTX_SWITCH;
2483 -               if (is_kernel)
2484 -                       flags |= IS_KERNEL;
2485 +       if (cpu_buf->last_cpu_mode != cpu_mode) {
2486 +               cpu_buf->last_cpu_mode = cpu_mode;
2487 +               flags |= KERNEL_CTX_SWITCH | cpu_mode;
2488         }
2489  
2490         /* notice a task switch */
2491 -       if (cpu_buf->last_task != task) {
2492 +       /* if not processing other domain samples */
2493 +       if (cpu_buf->last_task != task &&
2494 +           current_domain == COORDINATOR_DOMAIN) {
2495                 cpu_buf->last_task = task;
2496                 flags |= USER_CTX_SWITCH;
2497         }
2498 @@ -251,14 +256,14 @@ op_add_sample(struct oprofile_cpu_buffer
2499  /*
2500   * This must be safe from any context.
2501   *
2502 - * is_kernel is needed because on some architectures you cannot
2503 + * cpu_mode is needed because on some architectures you cannot
2504   * tell if you are in kernel or user space simply by looking at
2505 - * pc. We tag this in the buffer by generating kernel enter/exit
2506 - * events whenever is_kernel changes
2507 + * pc. We tag this in the buffer by generating kernel/user (and
2508 + * xen) enter events whenever cpu_mode changes
2509   */
2510  static int
2511  log_sample(struct oprofile_cpu_buffer *cpu_buf, unsigned long pc,
2512 -          unsigned long backtrace, int is_kernel, unsigned long event,
2513 +          unsigned long backtrace, int cpu_mode, unsigned long event,
2514            struct task_struct *task)
2515  {
2516         struct task_struct *tsk = task ? task : current;
2517 @@ -269,7 +274,7 @@ log_sample(struct oprofile_cpu_buffer *c
2518                 return 0;
2519         }
2520  
2521 -       if (op_add_code(cpu_buf, backtrace, is_kernel, tsk))
2522 +       if (op_add_code(cpu_buf, backtrace, cpu_mode, tsk))
2523                 goto fail;
2524  
2525         if (op_add_sample(cpu_buf, pc, event))
2526 @@ -440,6 +445,25 @@ fail:
2527         return;
2528  }
2529  
2530 +int oprofile_add_domain_switch(int32_t domain_id)
2531 +{
2532 +       struct oprofile_cpu_buffer * cpu_buf = &cpu_buffer[smp_processor_id()];
2533 +
2534 +       /* should have space for switching into and out of domain
2535 +          (2 slots each) plus one sample and one cpu mode switch */
2536 +       if (((nr_available_slots(cpu_buf) < 6) &&
2537 +            (domain_id != COORDINATOR_DOMAIN)) ||
2538 +           (nr_available_slots(cpu_buf) < 2))
2539 +               return 0;
2540 +
2541 +       add_code(cpu_buf, DOMAIN_SWITCH);
2542 +       add_sample(cpu_buf, domain_id, 0);
2543 +
2544 +       current_domain = domain_id;
2545 +
2546 +       return 1;
2547 +}
2548 +
2549  /*
2550   * This serves to avoid cpu buffer overflow, and makes sure
2551   * the task mortuary progresses
2552 --- head-2011-11-16.orig/drivers/oprofile/cpu_buffer.h  2011-01-05 01:50:19.000000000 +0100
2553 +++ head-2011-11-16/drivers/oprofile/cpu_buffer.h       2011-04-13 11:25:29.000000000 +0200
2554 @@ -41,7 +41,7 @@ struct op_entry;
2555  struct oprofile_cpu_buffer {
2556         unsigned long buffer_size;
2557         struct task_struct *last_task;
2558 -       int last_is_kernel;
2559 +       int last_cpu_mode;
2560         int tracing;
2561         unsigned long sample_received;
2562         unsigned long sample_lost_overflow;
2563 @@ -63,7 +63,7 @@ static inline void op_cpu_buffer_reset(i
2564  {
2565         struct oprofile_cpu_buffer *cpu_buf = &per_cpu(op_cpu_buffer, cpu);
2566  
2567 -       cpu_buf->last_is_kernel = -1;
2568 +       cpu_buf->last_cpu_mode = -1;
2569         cpu_buf->last_task = NULL;
2570  }
2571  
2572 @@ -113,9 +113,13 @@ int op_cpu_buffer_get_data(struct op_ent
2573  }
2574  
2575  /* extra data flags */
2576 -#define KERNEL_CTX_SWITCH      (1UL << 0)
2577 -#define IS_KERNEL              (1UL << 1)
2578 +#define CPU_MODE_USER          0
2579 +#define CPU_MODE_KERNEL                1
2580 +#define CPU_MODE_XEN           2
2581 +#define CPU_MODE_MASK          3
2582  #define TRACE_BEGIN            (1UL << 2)
2583  #define USER_CTX_SWITCH                (1UL << 3)
2584 +#define KERNEL_CTX_SWITCH      (1UL << 4)
2585 +#define DOMAIN_SWITCH          (1UL << 5)
2586  
2587  #endif /* OPROFILE_CPU_BUFFER_H */
2588 --- head-2011-11-16.orig/drivers/oprofile/event_buffer.h        2011-07-22 04:17:23.000000000 +0200
2589 +++ head-2011-11-16/drivers/oprofile/event_buffer.h     2011-04-13 11:25:29.000000000 +0200
2590 @@ -30,6 +30,9 @@ void wake_up_buffer_waiter(void);
2591  #define INVALID_COOKIE ~0UL
2592  #define NO_COOKIE 0UL
2593  
2594 +/* Constant used to refer to coordinator domain (Xen) */
2595 +#define COORDINATOR_DOMAIN -1
2596 +
2597  extern const struct file_operations event_buffer_fops;
2598  
2599  /* mutex between sync_cpu_buffers() and the
2600 --- head-2011-11-16.orig/drivers/oprofile/oprof.c       2011-07-22 04:17:23.000000000 +0200
2601 +++ head-2011-11-16/drivers/oprofile/oprof.c    2011-04-13 11:25:29.000000000 +0200
2602 @@ -5,6 +5,10 @@
2603   * @remark Read the file COPYING
2604   *
2605   * @author John Levon <levon@movementarian.org>
2606 + *
2607 + * Modified by Aravind Menon for Xen
2608 + * These modifications are:
2609 + * Copyright (C) 2005 Hewlett-Packard Co.
2610   */
2611  
2612  #include <linux/kernel.h>
2613 @@ -35,6 +39,32 @@ static DEFINE_MUTEX(start_mutex);
2614   */
2615  static int timer = 0;
2616  
2617 +int oprofile_set_active(int active_domains[], unsigned int adomains)
2618 +{
2619 +       int err;
2620 +
2621 +       if (!oprofile_ops.set_active)
2622 +               return -EINVAL;
2623 +
2624 +       mutex_lock(&start_mutex);
2625 +       err = oprofile_ops.set_active(active_domains, adomains);
2626 +       mutex_unlock(&start_mutex);
2627 +       return err;
2628 +}
2629 +
2630 +int oprofile_set_passive(int passive_domains[], unsigned int pdomains)
2631 +{
2632 +       int err;
2633 +
2634 +       if (!oprofile_ops.set_passive)
2635 +               return -EINVAL;
2636 +
2637 +       mutex_lock(&start_mutex);
2638 +       err = oprofile_ops.set_passive(passive_domains, pdomains);
2639 +       mutex_unlock(&start_mutex);
2640 +       return err;
2641 +}
2642 +
2643  int oprofile_setup(void)
2644  {
2645         int err;
2646 --- head-2011-11-16.orig/drivers/oprofile/oprof.h       2011-01-05 01:50:19.000000000 +0100
2647 +++ head-2011-11-16/drivers/oprofile/oprof.h    2011-04-13 11:25:29.000000000 +0200
2648 @@ -40,4 +40,7 @@ void oprofile_timer_exit(void);
2649  int oprofile_set_ulong(unsigned long *addr, unsigned long val);
2650  int oprofile_set_timeout(unsigned long time);
2651  
2652 +int oprofile_set_active(int active_domains[], unsigned int adomains);
2653 +int oprofile_set_passive(int passive_domains[], unsigned int pdomains);
2654 +
2655  #endif /* OPROF_H */
2656 --- head-2011-11-16.orig/drivers/oprofile/oprofile_files.c      2011-01-05 01:50:19.000000000 +0100
2657 +++ head-2011-11-16/drivers/oprofile/oprofile_files.c   2011-04-13 11:25:29.000000000 +0200
2658 @@ -5,11 +5,17 @@
2659   * @remark Read the file COPYING
2660   *
2661   * @author John Levon <levon@movementarian.org>
2662 + *
2663 + * Modified by Aravind Menon for Xen
2664 + * These modifications are:
2665 + * Copyright (C) 2005 Hewlett-Packard Co.
2666   */
2667  
2668  #include <linux/fs.h>
2669  #include <linux/oprofile.h>
2670  #include <linux/jiffies.h>
2671 +#include <asm/uaccess.h>
2672 +#include <linux/ctype.h>
2673  
2674  #include "event_buffer.h"
2675  #include "oprofile_stats.h"
2676 @@ -174,6 +180,195 @@ static const struct file_operations dump
2677         .llseek         = noop_llseek,
2678  };
2679  
2680 +#define TMPBUFSIZE 512
2681 +
2682 +static unsigned int adomains = 0;
2683 +static int active_domains[MAX_OPROF_DOMAINS + 1];
2684 +static DEFINE_MUTEX(adom_mutex);
2685 +
2686 +static ssize_t adomain_write(struct file * file, char const __user * buf,
2687 +                            size_t count, loff_t * offset)
2688 +{
2689 +       char *tmpbuf;
2690 +       char *startp, *endp;
2691 +       int i;
2692 +       unsigned long val;
2693 +       ssize_t retval = count;
2694 +
2695 +       if (*offset)
2696 +               return -EINVAL;
2697 +       if (count > TMPBUFSIZE - 1)
2698 +               return -EINVAL;
2699 +
2700 +       if (!(tmpbuf = kmalloc(TMPBUFSIZE, GFP_KERNEL)))
2701 +               return -ENOMEM;
2702 +
2703 +       if (copy_from_user(tmpbuf, buf, count)) {
2704 +               kfree(tmpbuf);
2705 +               return -EFAULT;
2706 +       }
2707 +       tmpbuf[count] = 0;
2708 +
2709 +       mutex_lock(&adom_mutex);
2710 +
2711 +       startp = tmpbuf;
2712 +       /* Parse one more than MAX_OPROF_DOMAINS, for easy error checking */
2713 +       for (i = 0; i <= MAX_OPROF_DOMAINS; i++) {
2714 +               val = simple_strtoul(startp, &endp, 0);
2715 +               if (endp == startp)
2716 +                       break;
2717 +               while (ispunct(*endp) || isspace(*endp))
2718 +                       endp++;
2719 +               active_domains[i] = val;
2720 +               if (active_domains[i] != val)
2721 +                       /* Overflow, force error below */
2722 +                       i = MAX_OPROF_DOMAINS + 1;
2723 +               startp = endp;
2724 +       }
2725 +       /* Force error on trailing junk */
2726 +       adomains = *startp ? MAX_OPROF_DOMAINS + 1 : i;
2727 +
2728 +       kfree(tmpbuf);
2729 +
2730 +       if (adomains > MAX_OPROF_DOMAINS
2731 +           || oprofile_set_active(active_domains, adomains)) {
2732 +               adomains = 0;
2733 +               retval = -EINVAL;
2734 +       }
2735 +
2736 +       mutex_unlock(&adom_mutex);
2737 +       return retval;
2738 +}
2739 +
2740 +static ssize_t adomain_read(struct file * file, char __user * buf,
2741 +                           size_t count, loff_t * offset)
2742 +{
2743 +       char * tmpbuf;
2744 +       size_t len;
2745 +       int i;
2746 +       ssize_t retval;
2747 +
2748 +       if (!(tmpbuf = kmalloc(TMPBUFSIZE, GFP_KERNEL)))
2749 +               return -ENOMEM;
2750 +
2751 +       mutex_lock(&adom_mutex);
2752 +
2753 +       len = 0;
2754 +       for (i = 0; i < adomains; i++)
2755 +               len += snprintf(tmpbuf + len,
2756 +                               len < TMPBUFSIZE ? TMPBUFSIZE - len : 0,
2757 +                               "%u ", active_domains[i]);
2758 +       WARN_ON(len > TMPBUFSIZE);
2759 +       if (len != 0 && len <= TMPBUFSIZE)
2760 +               tmpbuf[len-1] = '\n';
2761 +
2762 +       mutex_unlock(&adom_mutex);
2763 +
2764 +       retval = simple_read_from_buffer(buf, count, offset, tmpbuf, len);
2765 +
2766 +       kfree(tmpbuf);
2767 +       return retval;
2768 +}
2769 +
2770 +
2771 +static const struct file_operations active_domain_ops = {
2772 +       .read           = adomain_read,
2773 +       .write          = adomain_write,
2774 +};
2775 +
2776 +static unsigned int pdomains = 0;
2777 +static int passive_domains[MAX_OPROF_DOMAINS];
2778 +static DEFINE_MUTEX(pdom_mutex);
2779 +
2780 +static ssize_t pdomain_write(struct file * file, char const __user * buf,
2781 +                            size_t count, loff_t * offset)
2782 +{
2783 +       char *tmpbuf;
2784 +       char *startp, *endp;
2785 +       int i;
2786 +       unsigned long val;
2787 +       ssize_t retval = count;
2788 +
2789 +       if (*offset)
2790 +               return -EINVAL;
2791 +       if (count > TMPBUFSIZE - 1)
2792 +               return -EINVAL;
2793 +
2794 +       if (!(tmpbuf = kmalloc(TMPBUFSIZE, GFP_KERNEL)))
2795 +               return -ENOMEM;
2796 +
2797 +       if (copy_from_user(tmpbuf, buf, count)) {
2798 +               kfree(tmpbuf);
2799 +               return -EFAULT;
2800 +       }
2801 +       tmpbuf[count] = 0;
2802 +
2803 +       mutex_lock(&pdom_mutex);
2804 +
2805 +       startp = tmpbuf;
2806 +       /* Parse one more than MAX_OPROF_DOMAINS, for easy error checking */
2807 +       for (i = 0; i <= MAX_OPROF_DOMAINS; i++) {
2808 +               val = simple_strtoul(startp, &endp, 0);
2809 +               if (endp == startp)
2810 +                       break;
2811 +               while (ispunct(*endp) || isspace(*endp))
2812 +                       endp++;
2813 +               passive_domains[i] = val;
2814 +               if (passive_domains[i] != val)
2815 +                       /* Overflow, force error below */
2816 +                       i = MAX_OPROF_DOMAINS + 1;
2817 +               startp = endp;
2818 +       }
2819 +       /* Force error on trailing junk */
2820 +       pdomains = *startp ? MAX_OPROF_DOMAINS + 1 : i;
2821 +
2822 +       kfree(tmpbuf);
2823 +
2824 +       if (pdomains > MAX_OPROF_DOMAINS
2825 +           || oprofile_set_passive(passive_domains, pdomains)) {
2826 +               pdomains = 0;
2827 +               retval = -EINVAL;
2828 +       }
2829 +
2830 +       mutex_unlock(&pdom_mutex);
2831 +       return retval;
2832 +}
2833 +
2834 +static ssize_t pdomain_read(struct file * file, char __user * buf,
2835 +                           size_t count, loff_t * offset)
2836 +{
2837 +       char * tmpbuf;
2838 +       size_t len;
2839 +       int i;
2840 +       ssize_t retval;
2841 +
2842 +       if (!(tmpbuf = kmalloc(TMPBUFSIZE, GFP_KERNEL)))
2843 +               return -ENOMEM;
2844 +
2845 +       mutex_lock(&pdom_mutex);
2846 +
2847 +       len = 0;
2848 +       for (i = 0; i < pdomains; i++)
2849 +               len += snprintf(tmpbuf + len,
2850 +                               len < TMPBUFSIZE ? TMPBUFSIZE - len : 0,
2851 +                               "%u ", passive_domains[i]);
2852 +       WARN_ON(len > TMPBUFSIZE);
2853 +       if (len != 0 && len <= TMPBUFSIZE)
2854 +               tmpbuf[len-1] = '\n';
2855 +
2856 +       mutex_unlock(&pdom_mutex);
2857 +
2858 +       retval = simple_read_from_buffer(buf, count, offset, tmpbuf, len);
2859 +
2860 +       kfree(tmpbuf);
2861 +       return retval;
2862 +}
2863 +
2864 +static const struct file_operations passive_domain_ops = {
2865 +       .read           = pdomain_read,
2866 +       .write          = pdomain_write,
2867 +};
2868 +
2869  void oprofile_create_files(struct super_block *sb, struct dentry *root)
2870  {
2871         /* reinitialize default values */
2872 @@ -184,6 +379,8 @@ void oprofile_create_files(struct super_
2873  
2874         oprofilefs_create_file(sb, root, "enable", &enable_fops);
2875         oprofilefs_create_file_perm(sb, root, "dump", &dump_fops, 0666);
2876 +       oprofilefs_create_file(sb, root, "active_domains", &active_domain_ops);
2877 +       oprofilefs_create_file(sb, root, "passive_domains", &passive_domain_ops);
2878         oprofilefs_create_file(sb, root, "buffer", &event_buffer_fops);
2879         oprofilefs_create_ulong(sb, root, "buffer_size", &oprofile_buffer_size);
2880         oprofilefs_create_ulong(sb, root, "buffer_watershed", &oprofile_buffer_watershed);
2881 --- head-2011-11-16.orig/fs/aio.c       2011-11-16 16:26:44.000000000 +0100
2882 +++ head-2011-11-16/fs/aio.c    2011-11-16 17:01:54.000000000 +0100
2883 @@ -39,6 +39,11 @@
2884  #include <asm/kmap_types.h>
2885  #include <asm/uaccess.h>
2886  
2887 +#ifdef CONFIG_EPOLL
2888 +#include <linux/poll.h>
2889 +#include <linux/eventpoll.h>
2890 +#endif
2891 +
2892  #if DEBUG > 1
2893  #define dprintk                printk
2894  #else
2895 @@ -1064,6 +1069,11 @@ put_rq:
2896         if (waitqueue_active(&ctx->wait))
2897                 wake_up(&ctx->wait);
2898  
2899 +#ifdef CONFIG_EPOLL
2900 +       if (ctx->file && waitqueue_active(&ctx->poll_wait))
2901 +               wake_up(&ctx->poll_wait);
2902 +#endif
2903 +
2904         spin_unlock_irqrestore(&ctx->ctx_lock, flags);
2905         return ret;
2906  }
2907 @@ -1072,6 +1082,8 @@ EXPORT_SYMBOL(aio_complete);
2908  /* aio_read_evt
2909   *     Pull an event off of the ioctx's event ring.  Returns the number of 
2910   *     events fetched (0 or 1 ;-)
2911 + *     If ent parameter is 0, just returns the number of events that would
2912 + *     be fetched.
2913   *     FIXME: make this use cmpxchg.
2914   *     TODO: make the ringbuffer user mmap()able (requires FIXME).
2915   */
2916 @@ -1094,13 +1106,18 @@ static int aio_read_evt(struct kioctx *i
2917  
2918         head = ring->head % info->nr;
2919         if (head != ring->tail) {
2920 -               struct io_event *evp = aio_ring_event(info, head, KM_USER1);
2921 -               *ent = *evp;
2922 -               head = (head + 1) % info->nr;
2923 -               smp_mb(); /* finish reading the event before updatng the head */
2924 -               ring->head = head;
2925 -               ret = 1;
2926 -               put_aio_ring_event(evp, KM_USER1);
2927 +               if (ent) { /* event requested */
2928 +                       struct io_event *evp =
2929 +                               aio_ring_event(info, head, KM_USER1);
2930 +                       *ent = *evp;
2931 +                       head = (head + 1) % info->nr;
2932 +                       /* finish reading the event before updatng the head */
2933 +                       smp_mb();
2934 +                       ring->head = head;
2935 +                       ret = 1;
2936 +                       put_aio_ring_event(evp, KM_USER1);
2937 +               } else /* only need to know availability */
2938 +                       ret = 1;
2939         }
2940         spin_unlock(&info->ring_lock);
2941  
2942 @@ -1285,6 +1302,13 @@ static void io_destroy(struct kioctx *io
2943  
2944         aio_cancel_all(ioctx);
2945         wait_for_all_aios(ioctx);
2946 +#ifdef CONFIG_EPOLL
2947 +       /* forget the poll file, but it's up to the user to close it */
2948 +       if (ioctx->file) {
2949 +               ioctx->file->private_data = 0;
2950 +               ioctx->file = 0;
2951 +       }
2952 +#endif
2953  
2954         /*
2955          * Wake up any waiters.  The setting of ctx->dead must be seen
2956 @@ -1295,6 +1319,67 @@ static void io_destroy(struct kioctx *io
2957         put_ioctx(ioctx);       /* once for the lookup */
2958  }
2959  
2960 +#ifdef CONFIG_EPOLL
2961 +
2962 +static int aio_queue_fd_close(struct inode *inode, struct file *file)
2963 +{
2964 +       struct kioctx *ioctx = file->private_data;
2965 +       if (ioctx) {
2966 +               file->private_data = 0;
2967 +               spin_lock_irq(&ioctx->ctx_lock);
2968 +               ioctx->file = 0;
2969 +               spin_unlock_irq(&ioctx->ctx_lock);
2970 +       }
2971 +       return 0;
2972 +}
2973 +
2974 +static unsigned int aio_queue_fd_poll(struct file *file, poll_table *wait)
2975 +{      unsigned int pollflags = 0;
2976 +       struct kioctx *ioctx = file->private_data;
2977 +
2978 +       if (ioctx) {
2979 +
2980 +               spin_lock_irq(&ioctx->ctx_lock);
2981 +               /* Insert inside our poll wait queue */
2982 +               poll_wait(file, &ioctx->poll_wait, wait);
2983 +
2984 +               /* Check our condition */
2985 +               if (aio_read_evt(ioctx, 0))
2986 +                       pollflags = POLLIN | POLLRDNORM;
2987 +               spin_unlock_irq(&ioctx->ctx_lock);
2988 +       }
2989 +
2990 +       return pollflags;
2991 +}
2992 +
2993 +static const struct file_operations aioq_fops = {
2994 +       .release        = aio_queue_fd_close,
2995 +       .poll           = aio_queue_fd_poll
2996 +};
2997 +
2998 +/* make_aio_fd:
2999 + *  Create a file descriptor that can be used to poll the event queue.
3000 + *  Based and piggybacked on the excellent epoll code.
3001 + */
3002 +
3003 +static int make_aio_fd(struct kioctx *ioctx)
3004 +{
3005 +       int error, fd;
3006 +       struct inode *inode;
3007 +       struct file *file;
3008 +
3009 +       error = ep_getfd(&fd, &inode, &file, NULL, &aioq_fops);
3010 +       if (error)
3011 +               return error;
3012 +
3013 +       /* associate the file with the IO context */
3014 +       file->private_data = ioctx;
3015 +       ioctx->file = file;
3016 +       init_waitqueue_head(&ioctx->poll_wait);
3017 +       return fd;
3018 +}
3019 +#endif
3020 +
3021  /* sys_io_setup:
3022   *     Create an aio_context capable of receiving at least nr_events.
3023   *     ctxp must not point to an aio_context that already exists, and
3024 @@ -1307,18 +1392,30 @@ static void io_destroy(struct kioctx *io
3025   *     resources are available.  May fail with -EFAULT if an invalid
3026   *     pointer is passed for ctxp.  Will fail with -ENOSYS if not
3027   *     implemented.
3028 + *
3029 + *     To request a selectable fd, the user context has to be initialized
3030 + *     to 1, instead of 0, and the return value is the fd.
3031 + *     This keeps the system call compatible, since a non-zero value
3032 + *     was not allowed so far.
3033   */
3034  SYSCALL_DEFINE2(io_setup, unsigned, nr_events, aio_context_t __user *, ctxp)
3035  {
3036         struct kioctx *ioctx = NULL;
3037         unsigned long ctx;
3038         long ret;
3039 +       int make_fd = 0;
3040  
3041         ret = get_user(ctx, ctxp);
3042         if (unlikely(ret))
3043                 goto out;
3044  
3045         ret = -EINVAL;
3046 +#ifdef CONFIG_EPOLL
3047 +       if (ctx == 1) {
3048 +               make_fd = 1;
3049 +               ctx = 0;
3050 +       }
3051 +#endif
3052         if (unlikely(ctx || nr_events == 0)) {
3053                 pr_debug("EINVAL: io_setup: ctx %lu nr_events %u\n",
3054                          ctx, nr_events);
3055 @@ -1329,8 +1426,12 @@ SYSCALL_DEFINE2(io_setup, unsigned, nr_e
3056         ret = PTR_ERR(ioctx);
3057         if (!IS_ERR(ioctx)) {
3058                 ret = put_user(ioctx->user_id, ctxp);