2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for
16 #include <linux/proc_fs.h>
17 #include <linux/seq_file.h>
18 #include <linux/rwsem.h>
19 #include <linux/kprobes.h>
20 #include <linux/sched.h>
21 #include <linux/hardirq.h>
22 #include <linux/uaccess.h>
23 #include <linux/smp.h>
24 #include <linux/cdev.h>
25 #include <linux/compat.h>
26 #include <asm/hardwall.h>
27 #include <asm/traps.h>
28 #include <asm/siginfo.h>
29 #include <asm/irq_regs.h>
31 #include <arch/interrupts.h>
32 #include <arch/spr_def.h>
36 * Implement a per-cpu "hardwall" resource class such as UDN or IPI.
37 * We use "hardwall" nomenclature throughout for historical reasons.
38 * The lock here controls access to the list data structure as well as
39 * to the items on the list.
41 struct hardwall_type
{
47 struct list_head list
;
49 struct proc_dir_entry
*proc_dir
;
61 static struct hardwall_type hardwall_types
[] = {
62 { /* user-space access to UDN */
68 LIST_HEAD_INIT(hardwall_types
[HARDWALL_UDN
].list
),
69 __SPIN_LOCK_UNLOCKED(hardwall_types
[HARDWALL_UDN
].lock
),
73 { /* user-space access to IDN */
77 1, /* disabled pending hypervisor support */
79 LIST_HEAD_INIT(hardwall_types
[HARDWALL_IDN
].list
),
80 __SPIN_LOCK_UNLOCKED(hardwall_types
[HARDWALL_IDN
].lock
),
83 { /* access to user-space IPI */
89 LIST_HEAD_INIT(hardwall_types
[HARDWALL_IPI
].list
),
90 __SPIN_LOCK_UNLOCKED(hardwall_types
[HARDWALL_IPI
].lock
),
97 * This data structure tracks the cpu data, etc., associated
98 * one-to-one with a "struct file *" from opening a hardwall device file.
99 * Note that the file's private data points back to this structure.
101 struct hardwall_info
{
102 struct list_head list
; /* for hardwall_types.list */
103 struct list_head task_head
; /* head of tasks in this hardwall */
104 struct hardwall_type
*type
; /* type of this resource */
105 struct cpumask cpumask
; /* cpus reserved */
106 int id
; /* integer id for this hardwall */
107 int teardown_in_progress
; /* are we tearing this one down? */
109 /* Remaining fields only valid for user-network resources. */
110 int ulhc_x
; /* upper left hand corner x coord */
111 int ulhc_y
; /* upper left hand corner y coord */
112 int width
; /* rectangle width */
113 int height
; /* rectangle height */
114 #if CHIP_HAS_REV1_XDN()
115 atomic_t xdn_pending_count
; /* cores in phase 1 of drain */
120 /* /proc/tile/hardwall */
121 static struct proc_dir_entry
*hardwall_proc_dir
;
123 /* Functions to manage files in /proc/tile/hardwall. */
124 static void hardwall_add_proc(struct hardwall_info
*);
125 static void hardwall_remove_proc(struct hardwall_info
*);
127 /* Allow disabling UDN access. */
128 static int __init
noudn(char *str
)
130 pr_info("User-space UDN access is disabled\n");
131 hardwall_types
[HARDWALL_UDN
].disabled
= 1;
134 early_param("noudn", noudn
);
137 /* Allow disabling IDN access. */
138 static int __init
noidn(char *str
)
140 pr_info("User-space IDN access is disabled\n");
141 hardwall_types
[HARDWALL_IDN
].disabled
= 1;
144 early_param("noidn", noidn
);
146 /* Allow disabling IPI access. */
147 static int __init
noipi(char *str
)
149 pr_info("User-space IPI access is disabled\n");
150 hardwall_types
[HARDWALL_IPI
].disabled
= 1;
153 early_param("noipi", noipi
);
158 * Low-level primitives for UDN/IDN
162 #define mtspr_XDN(hwt, name, val) \
163 do { (void)(hwt); __insn_mtspr(SPR_UDN_##name, (val)); } while (0)
164 #define mtspr_MPL_XDN(hwt, name, val) \
165 do { (void)(hwt); __insn_mtspr(SPR_MPL_UDN_##name, (val)); } while (0)
166 #define mfspr_XDN(hwt, name) \
167 ((void)(hwt), __insn_mfspr(SPR_UDN_##name))
169 #define mtspr_XDN(hwt, name, val) \
172 __insn_mtspr(SPR_IDN_##name, (val)); \
174 __insn_mtspr(SPR_UDN_##name, (val)); \
176 #define mtspr_MPL_XDN(hwt, name, val) \
179 __insn_mtspr(SPR_MPL_IDN_##name, (val)); \
181 __insn_mtspr(SPR_MPL_UDN_##name, (val)); \
183 #define mfspr_XDN(hwt, name) \
184 ((hwt)->is_idn ? __insn_mfspr(SPR_IDN_##name) : __insn_mfspr(SPR_UDN_##name))
187 /* Set a CPU bit if the CPU is online. */
188 #define cpu_online_set(cpu, dst) do { \
189 if (cpu_online(cpu)) \
190 cpumask_set_cpu(cpu, dst); \
194 /* Does the given rectangle contain the given x,y coordinate? */
195 static int contains(struct hardwall_info
*r
, int x
, int y
)
197 return (x
>= r
->ulhc_x
&& x
< r
->ulhc_x
+ r
->width
) &&
198 (y
>= r
->ulhc_y
&& y
< r
->ulhc_y
+ r
->height
);
201 /* Compute the rectangle parameters and validate the cpumask. */
202 static int check_rectangle(struct hardwall_info
*r
, struct cpumask
*mask
)
204 int x
, y
, cpu
, ulhc
, lrhc
;
206 /* The first cpu is the ULHC, the last the LRHC. */
207 ulhc
= find_first_bit(cpumask_bits(mask
), nr_cpumask_bits
);
208 lrhc
= find_last_bit(cpumask_bits(mask
), nr_cpumask_bits
);
210 /* Compute the rectangle attributes from the cpus. */
211 r
->ulhc_x
= cpu_x(ulhc
);
212 r
->ulhc_y
= cpu_y(ulhc
);
213 r
->width
= cpu_x(lrhc
) - r
->ulhc_x
+ 1;
214 r
->height
= cpu_y(lrhc
) - r
->ulhc_y
+ 1;
216 /* Width and height must be positive */
217 if (r
->width
<= 0 || r
->height
<= 0)
220 /* Confirm that the cpumask is exactly the rectangle. */
221 for (y
= 0, cpu
= 0; y
< smp_height
; ++y
)
222 for (x
= 0; x
< smp_width
; ++x
, ++cpu
)
223 if (cpumask_test_cpu(cpu
, mask
) != contains(r
, x
, y
))
227 * Note that offline cpus can't be drained when this user network
228 * rectangle eventually closes. We used to detect this
229 * situation and print a warning, but it annoyed users and
230 * they ignored it anyway, so now we just return without a
237 * Hardware management of hardwall setup, teardown, trapping,
238 * and enabling/disabling PL0 access to the networks.
241 /* Bit field values to mask together for writes to SPR_XDN_DIRECTION_PROTECT */
242 enum direction_protect
{
243 N_PROTECT
= (1 << 0),
244 E_PROTECT
= (1 << 1),
245 S_PROTECT
= (1 << 2),
246 W_PROTECT
= (1 << 3),
247 C_PROTECT
= (1 << 4),
250 static inline int xdn_which_interrupt(struct hardwall_type
*hwt
)
254 return INT_IDN_FIREWALL
;
256 return INT_UDN_FIREWALL
;
259 static void enable_firewall_interrupts(struct hardwall_type
*hwt
)
261 arch_local_irq_unmask_now(xdn_which_interrupt(hwt
));
264 static void disable_firewall_interrupts(struct hardwall_type
*hwt
)
266 arch_local_irq_mask_now(xdn_which_interrupt(hwt
));
269 /* Set up hardwall on this cpu based on the passed hardwall_info. */
270 static void hardwall_setup_func(void *info
)
272 struct hardwall_info
*r
= info
;
273 struct hardwall_type
*hwt
= r
->type
;
275 int cpu
= smp_processor_id(); /* on_each_cpu disables preemption */
281 if (x
== r
->ulhc_x
+ r
->width
- 1)
285 if (y
== r
->ulhc_y
+ r
->height
- 1)
288 mtspr_XDN(hwt
, DIRECTION_PROTECT
, bits
);
289 enable_firewall_interrupts(hwt
);
292 /* Set up all cpus on edge of rectangle to enable/disable hardwall SPRs. */
293 static void hardwall_protect_rectangle(struct hardwall_info
*r
)
295 int x
, y
, cpu
, delta
;
296 struct cpumask rect_cpus
;
298 cpumask_clear(&rect_cpus
);
300 /* First include the top and bottom edges */
301 cpu
= r
->ulhc_y
* smp_width
+ r
->ulhc_x
;
302 delta
= (r
->height
- 1) * smp_width
;
303 for (x
= 0; x
< r
->width
; ++x
, ++cpu
) {
304 cpu_online_set(cpu
, &rect_cpus
);
305 cpu_online_set(cpu
+ delta
, &rect_cpus
);
308 /* Then the left and right edges */
310 delta
= r
->width
- 1;
311 for (y
= 0; y
< r
->height
; ++y
, cpu
+= smp_width
) {
312 cpu_online_set(cpu
, &rect_cpus
);
313 cpu_online_set(cpu
+ delta
, &rect_cpus
);
316 /* Then tell all the cpus to set up their protection SPR */
317 on_each_cpu_mask(&rect_cpus
, hardwall_setup_func
, r
, 1);
320 /* Entered from INT_xDN_FIREWALL interrupt vector with irqs disabled. */
321 void __kprobes
do_hardwall_trap(struct pt_regs
* regs
, int fault_num
)
323 struct hardwall_info
*rect
;
324 struct hardwall_type
*hwt
;
325 struct task_struct
*p
;
327 int cpu
= smp_processor_id();
329 struct pt_regs
*old_regs
= set_irq_regs(regs
);
333 /* Figure out which network trapped. */
336 case INT_IDN_FIREWALL
:
337 hwt
= &hardwall_types
[HARDWALL_IDN
];
340 case INT_UDN_FIREWALL
:
341 hwt
= &hardwall_types
[HARDWALL_UDN
];
346 BUG_ON(hwt
->disabled
);
348 /* This tile trapped a network access; find the rectangle. */
349 spin_lock(&hwt
->lock
);
350 list_for_each_entry(rect
, &hwt
->list
, list
) {
351 if (cpumask_test_cpu(cpu
, &rect
->cpumask
))
356 * It shouldn't be possible not to find this cpu on the
357 * rectangle list, since only cpus in rectangles get hardwalled.
358 * The hardwall is only removed after the user network is drained.
360 BUG_ON(&rect
->list
== &hwt
->list
);
363 * If we already started teardown on this hardwall, don't worry;
364 * the abort signal has been sent and we are just waiting for things
367 if (rect
->teardown_in_progress
) {
368 pr_notice("cpu %d: detected %s hardwall violation %#lx"
369 " while teardown already in progress\n",
371 (long)mfspr_XDN(hwt
, DIRECTION_PROTECT
));
376 * Kill off any process that is activated in this rectangle.
377 * We bypass security to deliver the signal, since it must be
378 * one of the activated processes that generated the user network
379 * message that caused this trap, and all the activated
380 * processes shared a single open file so are pretty tightly
381 * bound together from a security point of view to begin with.
383 rect
->teardown_in_progress
= 1;
384 wmb(); /* Ensure visibility of rectangle before notifying processes. */
385 pr_notice("cpu %d: detected %s hardwall violation %#lx...\n",
386 cpu
, hwt
->name
, (long)mfspr_XDN(hwt
, DIRECTION_PROTECT
));
387 info
.si_signo
= SIGILL
;
389 info
.si_code
= ILL_HARDWALL
;
391 list_for_each_entry(p
, &rect
->task_head
,
392 thread
.hardwall
[hwt
->index
].list
) {
393 BUG_ON(p
->thread
.hardwall
[hwt
->index
].info
!= rect
);
394 if (!(p
->flags
& PF_EXITING
)) {
396 pr_notice("hardwall: killing %d\n", p
->pid
);
397 do_send_sig_info(info
.si_signo
, &info
, p
, false);
400 if (!found_processes
)
401 pr_notice("hardwall: no associated processes!\n");
404 spin_unlock(&hwt
->lock
);
407 * We have to disable firewall interrupts now, or else when we
408 * return from this handler, we will simply re-interrupt back to
409 * it. However, we can't clear the protection bits, since we
410 * haven't yet drained the network, and that would allow packets
411 * to cross out of the hardwall region.
413 disable_firewall_interrupts(hwt
);
416 set_irq_regs(old_regs
);
419 /* Allow access from user space to the user network. */
420 void grant_hardwall_mpls(struct hardwall_type
*hwt
)
424 __insn_mtspr(SPR_MPL_IPI_0_SET_0
, 1);
428 mtspr_MPL_XDN(hwt
, ACCESS_SET_0
, 1);
429 mtspr_MPL_XDN(hwt
, AVAIL_SET_0
, 1);
430 mtspr_MPL_XDN(hwt
, COMPLETE_SET_0
, 1);
431 mtspr_MPL_XDN(hwt
, TIMER_SET_0
, 1);
432 #if !CHIP_HAS_REV1_XDN()
433 mtspr_MPL_XDN(hwt
, REFILL_SET_0
, 1);
434 mtspr_MPL_XDN(hwt
, CA_SET_0
, 1);
438 /* Deny access from user space to the user network. */
439 void restrict_hardwall_mpls(struct hardwall_type
*hwt
)
443 __insn_mtspr(SPR_MPL_IPI_0_SET_1
, 1);
447 mtspr_MPL_XDN(hwt
, ACCESS_SET_1
, 1);
448 mtspr_MPL_XDN(hwt
, AVAIL_SET_1
, 1);
449 mtspr_MPL_XDN(hwt
, COMPLETE_SET_1
, 1);
450 mtspr_MPL_XDN(hwt
, TIMER_SET_1
, 1);
451 #if !CHIP_HAS_REV1_XDN()
452 mtspr_MPL_XDN(hwt
, REFILL_SET_1
, 1);
453 mtspr_MPL_XDN(hwt
, CA_SET_1
, 1);
457 /* Restrict or deny as necessary for the task we're switching to. */
458 void hardwall_switch_tasks(struct task_struct
*prev
,
459 struct task_struct
*next
)
462 for (i
= 0; i
< HARDWALL_TYPES
; ++i
) {
463 if (prev
->thread
.hardwall
[i
].info
!= NULL
) {
464 if (next
->thread
.hardwall
[i
].info
== NULL
)
465 restrict_hardwall_mpls(&hardwall_types
[i
]);
466 } else if (next
->thread
.hardwall
[i
].info
!= NULL
) {
467 grant_hardwall_mpls(&hardwall_types
[i
]);
472 /* Does this task have the right to IPI the given cpu? */
473 int hardwall_ipi_valid(int cpu
)
476 struct hardwall_info
*info
=
477 current
->thread
.hardwall
[HARDWALL_IPI
].info
;
478 return info
&& cpumask_test_cpu(cpu
, &info
->cpumask
);
485 * Code to create, activate, deactivate, and destroy hardwall resources.
488 /* Create a hardwall for the given resource */
489 static struct hardwall_info
*hardwall_create(struct hardwall_type
*hwt
,
491 const unsigned char __user
*bits
)
493 struct hardwall_info
*iter
, *info
;
498 /* Reject crazy sizes out of hand, a la sys_mbind(). */
499 if (size
> PAGE_SIZE
)
500 return ERR_PTR(-EINVAL
);
502 /* Copy whatever fits into a cpumask. */
503 if (copy_from_user(&mask
, bits
, min(sizeof(struct cpumask
), size
)))
504 return ERR_PTR(-EFAULT
);
507 * If the size was short, clear the rest of the mask;
508 * otherwise validate that the rest of the user mask was zero
509 * (we don't try hard to be efficient when validating huge masks).
511 if (size
< sizeof(struct cpumask
)) {
512 memset((char *)&mask
+ size
, 0, sizeof(struct cpumask
) - size
);
513 } else if (size
> sizeof(struct cpumask
)) {
515 for (i
= sizeof(struct cpumask
); i
< size
; ++i
) {
517 if (get_user(c
, &bits
[i
]))
518 return ERR_PTR(-EFAULT
);
520 return ERR_PTR(-EINVAL
);
524 /* Allocate a new hardwall_info optimistically. */
525 info
= kmalloc(sizeof(struct hardwall_info
),
526 GFP_KERNEL
| __GFP_ZERO
);
528 return ERR_PTR(-ENOMEM
);
529 INIT_LIST_HEAD(&info
->task_head
);
532 /* Compute the rectangle size and validate that it's plausible. */
533 cpumask_copy(&info
->cpumask
, &mask
);
534 info
->id
= find_first_bit(cpumask_bits(&mask
), nr_cpumask_bits
);
536 rc
= check_rectangle(info
, &mask
);
544 * Eliminate cpus that are not part of this Linux client.
545 * Note that this allows for configurations that we might not want to
546 * support, such as one client on every even cpu, another client on
549 cpumask_and(&info
->cpumask
, &info
->cpumask
, cpu_online_mask
);
551 /* Confirm it doesn't overlap and add it to the list. */
552 spin_lock_irqsave(&hwt
->lock
, flags
);
553 list_for_each_entry(iter
, &hwt
->list
, list
) {
554 if (cpumask_intersects(&iter
->cpumask
, &info
->cpumask
)) {
555 spin_unlock_irqrestore(&hwt
->lock
, flags
);
557 return ERR_PTR(-EBUSY
);
560 list_add_tail(&info
->list
, &hwt
->list
);
561 spin_unlock_irqrestore(&hwt
->lock
, flags
);
563 /* Set up appropriate hardwalling on all affected cpus. */
565 hardwall_protect_rectangle(info
);
567 /* Create a /proc/tile/hardwall entry. */
568 hardwall_add_proc(info
);
573 /* Activate a given hardwall on this cpu for this process. */
574 static int hardwall_activate(struct hardwall_info
*info
)
578 struct task_struct
*p
= current
;
579 struct thread_struct
*ts
= &p
->thread
;
580 struct hardwall_type
*hwt
;
582 /* Require a hardwall. */
586 /* Not allowed to activate a hardwall that is being torn down. */
587 if (info
->teardown_in_progress
)
591 * Get our affinity; if we're not bound to this tile uniquely,
592 * we can't access the network registers.
594 if (cpumask_weight(&p
->cpus_allowed
) != 1)
597 /* Make sure we are bound to a cpu assigned to this resource. */
598 cpu
= smp_processor_id();
599 BUG_ON(cpumask_first(&p
->cpus_allowed
) != cpu
);
600 if (!cpumask_test_cpu(cpu
, &info
->cpumask
))
603 /* If we are already bound to this hardwall, it's a no-op. */
605 if (ts
->hardwall
[hwt
->index
].info
) {
606 BUG_ON(ts
->hardwall
[hwt
->index
].info
!= info
);
610 /* Success! This process gets to use the resource on this cpu. */
611 ts
->hardwall
[hwt
->index
].info
= info
;
612 spin_lock_irqsave(&hwt
->lock
, flags
);
613 list_add(&ts
->hardwall
[hwt
->index
].list
, &info
->task_head
);
614 spin_unlock_irqrestore(&hwt
->lock
, flags
);
615 grant_hardwall_mpls(hwt
);
616 printk(KERN_DEBUG
"Pid %d (%s) activated for %s hardwall: cpu %d\n",
617 p
->pid
, p
->comm
, hwt
->name
, cpu
);
622 * Deactivate a task's hardwall. Must hold lock for hardwall_type.
623 * This method may be called from exit_thread(), so we don't want to
624 * rely on too many fields of struct task_struct still being valid.
625 * We assume the cpus_allowed, pid, and comm fields are still valid.
627 static void _hardwall_deactivate(struct hardwall_type
*hwt
,
628 struct task_struct
*task
)
630 struct thread_struct
*ts
= &task
->thread
;
632 if (cpumask_weight(&task
->cpus_allowed
) != 1) {
633 pr_err("pid %d (%s) releasing %s hardwall with"
634 " an affinity mask containing %d cpus!\n",
635 task
->pid
, task
->comm
, hwt
->name
,
636 cpumask_weight(&task
->cpus_allowed
));
640 BUG_ON(ts
->hardwall
[hwt
->index
].info
== NULL
);
641 ts
->hardwall
[hwt
->index
].info
= NULL
;
642 list_del(&ts
->hardwall
[hwt
->index
].list
);
644 restrict_hardwall_mpls(hwt
);
647 /* Deactivate a task's hardwall. */
648 static int hardwall_deactivate(struct hardwall_type
*hwt
,
649 struct task_struct
*task
)
654 spin_lock_irqsave(&hwt
->lock
, flags
);
655 activated
= (task
->thread
.hardwall
[hwt
->index
].info
!= NULL
);
657 _hardwall_deactivate(hwt
, task
);
658 spin_unlock_irqrestore(&hwt
->lock
, flags
);
663 printk(KERN_DEBUG
"Pid %d (%s) deactivated for %s hardwall: cpu %d\n",
664 task
->pid
, task
->comm
, hwt
->name
, raw_smp_processor_id());
668 void hardwall_deactivate_all(struct task_struct
*task
)
671 for (i
= 0; i
< HARDWALL_TYPES
; ++i
)
672 if (task
->thread
.hardwall
[i
].info
)
673 hardwall_deactivate(&hardwall_types
[i
], task
);
676 /* Stop the switch before draining the network. */
677 static void stop_xdn_switch(void *arg
)
679 #if !CHIP_HAS_REV1_XDN()
680 /* Freeze the switch and the demux. */
681 __insn_mtspr(SPR_UDN_SP_FREEZE
,
682 SPR_UDN_SP_FREEZE__SP_FRZ_MASK
|
683 SPR_UDN_SP_FREEZE__DEMUX_FRZ_MASK
|
684 SPR_UDN_SP_FREEZE__NON_DEST_EXT_MASK
);
687 * Drop all packets bound for the core or off the edge.
688 * We rely on the normal hardwall protection setup code
689 * to have set the low four bits to trigger firewall interrupts,
690 * and shift those bits up to trigger "drop on send" semantics,
691 * plus adding "drop on send to core" for all switches.
692 * In practice it seems the switches latch the DIRECTION_PROTECT
693 * SPR so they won't start dropping if they're already
694 * delivering the last message to the core, but it doesn't
695 * hurt to enable it here.
697 struct hardwall_type
*hwt
= arg
;
698 unsigned long protect
= mfspr_XDN(hwt
, DIRECTION_PROTECT
);
699 mtspr_XDN(hwt
, DIRECTION_PROTECT
, (protect
| C_PROTECT
) << 5);
703 static void empty_xdn_demuxes(struct hardwall_type
*hwt
)
707 while (__insn_mfspr(SPR_IDN_DATA_AVAIL
) & (1 << 0))
708 (void) __tile_idn0_receive();
709 while (__insn_mfspr(SPR_IDN_DATA_AVAIL
) & (1 << 1))
710 (void) __tile_idn1_receive();
714 while (__insn_mfspr(SPR_UDN_DATA_AVAIL
) & (1 << 0))
715 (void) __tile_udn0_receive();
716 while (__insn_mfspr(SPR_UDN_DATA_AVAIL
) & (1 << 1))
717 (void) __tile_udn1_receive();
718 while (__insn_mfspr(SPR_UDN_DATA_AVAIL
) & (1 << 2))
719 (void) __tile_udn2_receive();
720 while (__insn_mfspr(SPR_UDN_DATA_AVAIL
) & (1 << 3))
721 (void) __tile_udn3_receive();
724 /* Drain all the state from a stopped switch. */
725 static void drain_xdn_switch(void *arg
)
727 struct hardwall_info
*info
= arg
;
728 struct hardwall_type
*hwt
= info
->type
;
730 #if CHIP_HAS_REV1_XDN()
732 * The switches have been configured to drop any messages
733 * destined for cores (or off the edge of the rectangle).
734 * But the current message may continue to be delivered,
735 * so we wait until all the cores have finished any pending
736 * messages before we stop draining.
738 int pending
= mfspr_XDN(hwt
, PENDING
);
740 empty_xdn_demuxes(hwt
);
746 atomic_dec(&info
->xdn_pending_count
);
747 while (atomic_read(&info
->xdn_pending_count
))
748 empty_xdn_demuxes(hwt
);
751 int from_tile_words
, ca_count
;
753 /* Empty out the 5 switch point fifos. */
754 for (i
= 0; i
< 5; i
++) {
756 __insn_mtspr(SPR_UDN_SP_FIFO_SEL
, i
);
757 words
= __insn_mfspr(SPR_UDN_SP_STATE
) & 0xF;
758 for (j
= 0; j
< words
; j
++)
759 (void) __insn_mfspr(SPR_UDN_SP_FIFO_DATA
);
760 BUG_ON((__insn_mfspr(SPR_UDN_SP_STATE
) & 0xF) != 0);
763 /* Dump out the 3 word fifo at top. */
764 from_tile_words
= (__insn_mfspr(SPR_UDN_DEMUX_STATUS
) >> 10) & 0x3;
765 for (i
= 0; i
< from_tile_words
; i
++)
766 (void) __insn_mfspr(SPR_UDN_DEMUX_WRITE_FIFO
);
768 /* Empty out demuxes. */
769 empty_xdn_demuxes(hwt
);
771 /* Empty out catch all. */
772 ca_count
= __insn_mfspr(SPR_UDN_DEMUX_CA_COUNT
);
773 for (i
= 0; i
< ca_count
; i
++)
774 (void) __insn_mfspr(SPR_UDN_CA_DATA
);
775 BUG_ON(__insn_mfspr(SPR_UDN_DEMUX_CA_COUNT
) != 0);
777 /* Clear demux logic. */
778 __insn_mtspr(SPR_UDN_DEMUX_CTL
, 1);
781 * Write switch state; experimentation indicates that 0xc3000
782 * is an idle switch point.
784 for (i
= 0; i
< 5; i
++) {
785 __insn_mtspr(SPR_UDN_SP_FIFO_SEL
, i
);
786 __insn_mtspr(SPR_UDN_SP_STATE
, 0xc3000);
791 /* Reset random XDN state registers at boot up and during hardwall teardown. */
792 static void reset_xdn_network_state(struct hardwall_type
*hwt
)
797 /* Clear out other random registers so we have a clean slate. */
798 mtspr_XDN(hwt
, DIRECTION_PROTECT
, 0);
799 mtspr_XDN(hwt
, AVAIL_EN
, 0);
800 mtspr_XDN(hwt
, DEADLOCK_TIMEOUT
, 0);
802 #if !CHIP_HAS_REV1_XDN()
803 /* Reset UDN coordinates to their standard value */
805 unsigned int cpu
= smp_processor_id();
806 unsigned int x
= cpu_x(cpu
);
807 unsigned int y
= cpu_y(cpu
);
808 __insn_mtspr(SPR_UDN_TILE_COORD
, (x
<< 18) | (y
<< 7));
811 /* Set demux tags to predefined values and enable them. */
812 __insn_mtspr(SPR_UDN_TAG_VALID
, 0xf);
813 __insn_mtspr(SPR_UDN_TAG_0
, (1 << 0));
814 __insn_mtspr(SPR_UDN_TAG_1
, (1 << 1));
815 __insn_mtspr(SPR_UDN_TAG_2
, (1 << 2));
816 __insn_mtspr(SPR_UDN_TAG_3
, (1 << 3));
818 /* Set other rev0 random registers to a clean state. */
819 __insn_mtspr(SPR_UDN_REFILL_EN
, 0);
820 __insn_mtspr(SPR_UDN_DEMUX_QUEUE_SEL
, 0);
821 __insn_mtspr(SPR_UDN_SP_FIFO_SEL
, 0);
823 /* Start the switch and demux. */
824 __insn_mtspr(SPR_UDN_SP_FREEZE
, 0);
828 void reset_network_state(void)
830 reset_xdn_network_state(&hardwall_types
[HARDWALL_UDN
]);
832 reset_xdn_network_state(&hardwall_types
[HARDWALL_IDN
]);
836 /* Restart an XDN switch after draining. */
837 static void restart_xdn_switch(void *arg
)
839 struct hardwall_type
*hwt
= arg
;
841 #if CHIP_HAS_REV1_XDN()
842 /* One last drain step to avoid races with injection and draining. */
843 empty_xdn_demuxes(hwt
);
846 reset_xdn_network_state(hwt
);
848 /* Disable firewall interrupts. */
849 disable_firewall_interrupts(hwt
);
852 /* Last reference to a hardwall is gone, so clear the network. */
853 static void hardwall_destroy(struct hardwall_info
*info
)
855 struct task_struct
*task
;
856 struct hardwall_type
*hwt
;
859 /* Make sure this file actually represents a hardwall. */
864 * Deactivate any remaining tasks. It's possible to race with
865 * some other thread that is exiting and hasn't yet called
866 * deactivate (when freeing its thread_info), so we carefully
867 * deactivate any remaining tasks before freeing the
868 * hardwall_info object itself.
871 info
->teardown_in_progress
= 1;
872 spin_lock_irqsave(&hwt
->lock
, flags
);
873 list_for_each_entry(task
, &info
->task_head
,
874 thread
.hardwall
[hwt
->index
].list
)
875 _hardwall_deactivate(hwt
, task
);
876 spin_unlock_irqrestore(&hwt
->lock
, flags
);
879 /* Configure the switches for draining the user network. */
881 "Clearing %s hardwall rectangle %dx%d %d,%d\n",
882 hwt
->name
, info
->width
, info
->height
,
883 info
->ulhc_x
, info
->ulhc_y
);
884 on_each_cpu_mask(&info
->cpumask
, stop_xdn_switch
, hwt
, 1);
886 /* Drain the network. */
887 #if CHIP_HAS_REV1_XDN()
888 atomic_set(&info
->xdn_pending_count
,
889 cpumask_weight(&info
->cpumask
));
890 on_each_cpu_mask(&info
->cpumask
, drain_xdn_switch
, info
, 0);
892 on_each_cpu_mask(&info
->cpumask
, drain_xdn_switch
, info
, 1);
895 /* Restart switch and disable firewall. */
896 on_each_cpu_mask(&info
->cpumask
, restart_xdn_switch
, hwt
, 1);
899 /* Remove the /proc/tile/hardwall entry. */
900 hardwall_remove_proc(info
);
902 /* Now free the hardwall from the list. */
903 spin_lock_irqsave(&hwt
->lock
, flags
);
904 BUG_ON(!list_empty(&info
->task_head
));
905 list_del(&info
->list
);
906 spin_unlock_irqrestore(&hwt
->lock
, flags
);
911 static int hardwall_proc_show(struct seq_file
*sf
, void *v
)
913 struct hardwall_info
*info
= sf
->private;
916 int rc
= cpulist_scnprintf(buf
, sizeof(buf
), &info
->cpumask
);
918 seq_write(sf
, buf
, rc
);
922 static int hardwall_proc_open(struct inode
*inode
,
925 return single_open(file
, hardwall_proc_show
, PDE_DATA(inode
));
928 static const struct file_operations hardwall_proc_fops
= {
929 .open
= hardwall_proc_open
,
932 .release
= single_release
,
935 static void hardwall_add_proc(struct hardwall_info
*info
)
938 snprintf(buf
, sizeof(buf
), "%d", info
->id
);
939 proc_create_data(buf
, 0444, info
->type
->proc_dir
,
940 &hardwall_proc_fops
, info
);
943 static void hardwall_remove_proc(struct hardwall_info
*info
)
946 snprintf(buf
, sizeof(buf
), "%d", info
->id
);
947 remove_proc_entry(buf
, info
->type
->proc_dir
);
950 int proc_pid_hardwall(struct task_struct
*task
, char *buffer
)
954 for (i
= 0; i
< HARDWALL_TYPES
; ++i
) {
955 struct hardwall_info
*info
= task
->thread
.hardwall
[i
].info
;
957 n
+= sprintf(&buffer
[n
], "%s: %d\n",
958 info
->type
->name
, info
->id
);
963 void proc_tile_hardwall_init(struct proc_dir_entry
*root
)
966 for (i
= 0; i
< HARDWALL_TYPES
; ++i
) {
967 struct hardwall_type
*hwt
= &hardwall_types
[i
];
970 if (hardwall_proc_dir
== NULL
)
971 hardwall_proc_dir
= proc_mkdir("hardwall", root
);
972 hwt
->proc_dir
= proc_mkdir(hwt
->name
, hardwall_proc_dir
);
978 * Character device support via ioctl/close.
981 static long hardwall_ioctl(struct file
*file
, unsigned int a
, unsigned long b
)
983 struct hardwall_info
*info
= file
->private_data
;
984 int minor
= iminor(file
->f_mapping
->host
);
985 struct hardwall_type
* hwt
;
987 if (_IOC_TYPE(a
) != HARDWALL_IOCTL_BASE
)
990 BUILD_BUG_ON(HARDWALL_TYPES
!= _HARDWALL_TYPES
);
991 BUILD_BUG_ON(HARDWALL_TYPES
!=
992 sizeof(hardwall_types
)/sizeof(hardwall_types
[0]));
994 if (minor
< 0 || minor
>= HARDWALL_TYPES
)
996 hwt
= &hardwall_types
[minor
];
997 WARN_ON(info
&& hwt
!= info
->type
);
999 switch (_IOC_NR(a
)) {
1000 case _HARDWALL_CREATE
:
1005 info
= hardwall_create(hwt
, _IOC_SIZE(a
),
1006 (const unsigned char __user
*)b
);
1008 return PTR_ERR(info
);
1009 file
->private_data
= info
;
1012 case _HARDWALL_ACTIVATE
:
1013 return hardwall_activate(info
);
1015 case _HARDWALL_DEACTIVATE
:
1016 if (current
->thread
.hardwall
[hwt
->index
].info
!= info
)
1018 return hardwall_deactivate(hwt
, current
);
1020 case _HARDWALL_GET_ID
:
1021 return info
? info
->id
: -EINVAL
;
1028 #ifdef CONFIG_COMPAT
1029 static long hardwall_compat_ioctl(struct file
*file
,
1030 unsigned int a
, unsigned long b
)
1032 /* Sign-extend the argument so it can be used as a pointer. */
1033 return hardwall_ioctl(file
, a
, (unsigned long)compat_ptr(b
));
1037 /* The user process closed the file; revoke access to user networks. */
1038 static int hardwall_flush(struct file
*file
, fl_owner_t owner
)
1040 struct hardwall_info
*info
= file
->private_data
;
1041 struct task_struct
*task
, *tmp
;
1042 unsigned long flags
;
1046 * NOTE: if multiple threads are activated on this hardwall
1047 * file, the other threads will continue having access to the
1048 * user network until they are context-switched out and back
1051 * NOTE: A NULL files pointer means the task is being torn
1052 * down, so in that case we also deactivate it.
1054 struct hardwall_type
*hwt
= info
->type
;
1055 spin_lock_irqsave(&hwt
->lock
, flags
);
1056 list_for_each_entry_safe(task
, tmp
, &info
->task_head
,
1057 thread
.hardwall
[hwt
->index
].list
) {
1058 if (task
->files
== owner
|| task
->files
== NULL
)
1059 _hardwall_deactivate(hwt
, task
);
1061 spin_unlock_irqrestore(&hwt
->lock
, flags
);
1067 /* This hardwall is gone, so destroy it. */
1068 static int hardwall_release(struct inode
*inode
, struct file
*file
)
1070 hardwall_destroy(file
->private_data
);
1074 static const struct file_operations dev_hardwall_fops
= {
1075 .open
= nonseekable_open
,
1076 .unlocked_ioctl
= hardwall_ioctl
,
1077 #ifdef CONFIG_COMPAT
1078 .compat_ioctl
= hardwall_compat_ioctl
,
1080 .flush
= hardwall_flush
,
1081 .release
= hardwall_release
,
1084 static struct cdev hardwall_dev
;
1086 static int __init
dev_hardwall_init(void)
1091 rc
= alloc_chrdev_region(&dev
, 0, HARDWALL_TYPES
, "hardwall");
1094 cdev_init(&hardwall_dev
, &dev_hardwall_fops
);
1095 rc
= cdev_add(&hardwall_dev
, dev
, HARDWALL_TYPES
);
1101 late_initcall(dev_hardwall_init
);