Skip to contentSkip to conversation

Add ITS support

RFC PATCH Superseded [email protected] 6 participants 88 messages Jun 22, 2015 – Jul 8, 2015

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v3 cover Add ITS support

From: Vijaya Kumar K <[email protected]>

This is based on DraftF version
http://xenbits.xen.org/people/ianc/vits/draftF.pdf

Following major features are supported
- GICv3 ITS support for arm64 platform
- Only Dom0 is supported

Basic boot is tested with single ITS node with hacks in pci driver. All Comments from V2 version are not fixed as this series is based on new design.

Vijaya Kumar K (18):
xen/arm: Add bitmap_find_next_zero_area helper function
xen: Add log2 functionality
xen: console: Add ratelimit support for error message xen/arm: gicv3: Refactor redistributor information xen/arm: ITS: Port ITS driver to xen
xen/arm: ITS: Add helper functions to manage its_devices xen/arm: ITS: implement hw_irq_controller for LPIs xen/arm: vITS: Add virtual ITS driver
xen/arm: ITS: Add virtual ITS commands support xen/arm: ITS: Add APIs to add and assign device xen/arm: ITS: Add GITS registers emulation xen/arm: ITS: Add GICR register emulation xen/arm: ITS: Add irq descriptors for LPIs xen/arm: ITS: Initialize physical ITS
xen/arm: ITS: Add domain specific ITS initialization xen/arm: ITS: Handle LPI interrupts
xen/arm: ITS: Generate ITS node for Dom0 xen/arm: ITS: Map ITS translation space

 xen/arch/arm/Makefile             |    2 +
 xen/arch/arm/domain_build.c       |   50 +-
 xen/arch/arm/gic-v3-its.c         | 1476 +++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c             |   71 +-
 xen/arch/arm/gic.c                |   32 +-
 xen/arch/arm/irq.c                |   54 +-
 xen/arch/arm/setup.c              |    1 +
 xen/arch/arm/vgic-v3-its.c        | 1259 +++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c            |   70 +-
 xen/arch/arm/vgic.c               |   90 ++-
 xen/common/bitmap.c               |   39 +
 xen/drivers/char/console.c        |   17 +-
 xen/include/asm-arm/domain.h      |    5 +
 xen/include/asm-arm/gic-its.h     |  306 ++++++++
 xen/include/asm-arm/gic.h         |   17 +
 xen/include/asm-arm/gic_v3_defs.h |  143 +++-
 xen/include/asm-arm/irq.h         |    4 +-
 xen/include/asm-arm/vgic.h        |    2 +
 xen/include/xen/bitmap.h          |    5 +
 xen/include/xen/bitops.h          |    8 +
 xen/include/xen/config.h          |    8 +-
 xen/include/xen/log2.h            |  205 ++++++
 22 files changed, 3809 insertions(+), 55 deletions(-)
 create mode 100644 xen/arch/arm/gic-v3-its.c
 create mode 100644 xen/arch/arm/vgic-v3-its.c
 create mode 100644 xen/include/asm-arm/gic-its.h
 create mode 100644 xen/include/xen/log2.h
Signature · 1 line
1.7.9.5
#1 lore raw

RFC PATCH v3 1/18 xen/arm: Add bitmap_find_next_zero_area helper function

From: Vijaya Kumar K <[email protected]>

bitmap_find_next_zero_area helper function will be used by physical ITS driver imported from linux

---
v3: Moved changes to xen/common/bitmap.c and
xen/include/xen/bitmap.h

---
 xen/common/bitmap.c      |   39 +++++++++++++++++++++++++++++++++++++++
 xen/include/xen/bitmap.h |    5 +++++
 2 files changed, 44 insertions(+)
xen/common/bitmap.c +39 -0
index 61d1ea4..ba060b2 100644
@@ -489,6 +489,45 @@ int bitmap_allocate_region(unsigned long *bitmap, int pos, int order)
 }
 EXPORT_SYMBOL(bitmap_allocate_region);
 
+/*
+ * bitmap_find_next_zero_area - find a contiguous aligned zero area
+ * @map: The address to base the search on
+ * @size: The bitmap size in bits
+ * @start: The bitnumber to start searching at
+ * @nr: The number of zeroed bits we're looking for
+ * @align_mask: Alignment mask for zero area
+ *
+ * The @align_mask should be one less than a power of 2; the effect is that
+ * the bit offset of all zero areas this function finds is multiples of that
+ * power of 2. A @align_mask of 0 means no alignment is required.
+ */
+#define ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
+
+unsigned long bitmap_find_next_zero_area(unsigned long *map,
+                                         unsigned long size,
+                                         unsigned long start,
+                                         unsigned int nr,
+                                         unsigned long align_mask)
+{
+        unsigned long index, end, i;
+again:
+        index = find_next_zero_bit(map, size, start);
+
+        /* Align allocation */
+        index = ALIGN_MASK(index, align_mask);
+
+        end = index + nr;
+        if (end > size)
+                return end;
+        i = find_next_bit(map, end, index);
+        if (i < end) {
+                start = i + 1;
+                goto again;
+        }
+        return index;
+}
+EXPORT_SYMBOL(bitmap_find_next_zero_area);
+
 #ifdef __BIG_ENDIAN
 
 void bitmap_long_to_byte(uint8_t *bp, const unsigned long *lp, int nbits)
xen/include/xen/bitmap.h +5 -0
index e2a3686..5c48ac3 100644
@@ -101,6 +101,11 @@ extern int bitmap_scnlistprintf(char *buf, unsigned int len,
 extern int bitmap_find_free_region(unsigned long *bitmap, int bits, int order);
 extern void bitmap_release_region(unsigned long *bitmap, int pos, int order);
 extern int bitmap_allocate_region(unsigned long *bitmap, int pos, int order);
+extern unsigned long bitmap_find_next_zero_area(unsigned long *map,
+                                                unsigned long size,
+                                                unsigned long start,
+                                                unsigned int nr,
+                                                unsigned long align_mask);
 
 #define BITMAP_LAST_WORD_MASK(nbits)					\
 (									\
Signature · 1 line
1.7.9.5
#2 lore raw

Re: [RFC PATCH v3 01/18] xen/arm: Add bitmap_find_next_zero_area helper function

Hi,

Please include all the relevant maintainers. See scripts/get_maintainers.pl.

Quoted 25 lines · On 22/06/15 13:01, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

bitmap_find_next_zero_area helper function will be used by physical ITS driver imported from linux

---
v3: Moved changes to xen/common/bitmap.c and
xen/include/xen/bitmap.h

---
 xen/common/bitmap.c      |   39 +++++++++++++++++++++++++++++++++++++++
 xen/include/xen/bitmap.h |    5 +++++
 2 files changed, 44 insertions(+)
xen/common/bitmap.c +3 -0
index 61d1ea4..ba060b2 100644
@@ -489,6 +489,45 @@ int bitmap_allocate_region(unsigned long *bitmap, int pos, int order)
 }
 EXPORT_SYMBOL(bitmap_allocate_region);
 
+/*
+ * bitmap_find_next_zero_area - find a contiguous aligned zero area
+ * @map: The address to base the search on

xen/common/bitmap.c int bitmap_allocate_region(unsigned long *bitmap, int pos, int order) · line 504

Quoted 10 lines
xen/common/bitmap.c +10 -0
+ * @size: The bitmap size in bits
+ * @start: The bitnumber to start searching at
+ * @nr: The number of zeroed bits we're looking for
+ * @align_mask: Alignment mask for zero area
+ *
+ * The @align_mask should be one less than a power of 2; the effect is that
+ * the bit offset of all zero areas this function finds is multiples of that
+ * power of 2. A @align_mask of 0 means no alignment is required.
+ */
+#define ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))

This is not bitmap specific, I would move this macro in lib.h

Quoted 15 lines
xen/common/bitmap.c +15 -0
+unsigned long bitmap_find_next_zero_area(unsigned long *map,
+                                         unsigned long size,
+                                         unsigned long start,
+                                         unsigned int nr,
+                                         unsigned long align_mask)
+{
+        unsigned long index, end, i;
+again:
+        index = find_next_zero_bit(map, size, start);
+
+        /* Align allocation */
+        index = ALIGN_MASK(index, align_mask);
+
+        end = index + nr;
+        if (end > size)

xen/common/bitmap.c int bitmap_allocate_region(unsigned long *bitmap, int pos, int order) · line 530

Quoted 10 lines
xen/common/bitmap.c +10 -0
+                return end;
+        i = find_next_bit(map, end, index);
+        if (i < end) {
+                start = i + 1;
+                goto again;
+        }
+        return index;
+}
+EXPORT_SYMBOL(bitmap_find_next_zero_area);
+

bitmap.c is using Linux coding style which include hard tabs. Please retain them.

Regards,

Signature · 1 line
Julien Grall
#3 lore raw

RFC PATCH v3 2/18 xen: Add log2 functionality

In reply to [email protected]

From: Vijaya Kumar K <[email protected]>

log2 helper apis are ported from linux

---
 xen/include/xen/bitops.h |    8 ++
 xen/include/xen/log2.h   |  205 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 213 insertions(+)
xen/include/xen/bitops.h +8 -0
index cb56f24..b01333c 100644
@@ -117,6 +117,14 @@ static inline int generic_fls64(__u64 x)
 # endif
 #endif
 
+static inline unsigned fls_long(unsigned long l)
+{
+    if (sizeof(l) == 4)
+        return fls(l);
+
+    return fls64(l);
+}
+
 static __inline__ int get_bitmask_order(unsigned int count)
 {
     int order;
xen/include/xen/log2.h new file +205 -0

205 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#4 lore raw

Re: [RFC PATCH v3 02/18] xen: Add log2 functionality

Quoted 1 line
Quoted 1 line
Quoted 1 line · On 22.06.15 at 14:01, <[email protected]> wrote:

First of all, please Cc _all_ relevant maintainers.

xen/include/xen/bitops.h static inline int generic_fls64(__u64 x) · line 126

Quoted 13 lines
xen/include/xen/bitops.h +7 -0
@@ -117,6 +117,14 @@ static inline int generic_fls64(__u64 x)
 # endif
 #endif
 
+static inline unsigned fls_long(unsigned long l)
+{
+    if (sizeof(l) == 4)
+        return fls(l);
+
+    return fls64(l);
+}

I'm not really opposed to this, but did you really verify that there's no suitable functionality in tree already (even if named differently)? I can't, e.g., see why flsl() wouldn't fit your needs.

Quoted 5 lines
xen/include/xen/log2.h new file +2 -0
@@ -0,0 +1,205 @@
+/* 
+ * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.

xen/include/xen/log2.h line 12

Quoted 10 lines
xen/include/xen/log2.h new file +10 -0
+ * Written by David Howells ([email protected])
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ */
+
+#ifndef _LINUX_LOG2_H
+#define _LINUX_LOG2_H

LINUX?

xen/include/xen/log2.h line 21

Quoted 5 lines
xen/include/xen/log2.h new file +5 -0
+/*
+ * deal with unrepresentable constant logarithms
+ */
+extern __attribute__((const))
+int ____ilog2_NaN(void);

-ETOOMANYUNDERSCORES

xen/include/xen/log2.h line 18

Quoted 1 line
xen/include/xen/log2.h new file +1 -0
+#if 1

??? (at least one more below)

Also, are you really needing all of what you add here?

Jan

#5 lore raw

Re: [RFC PATCH v3 02/18] xen: Add log2 functionality

Quoted 10 lines · Jan Beulich
Quoted 1 line
Quoted 1 line
Quoted 1 line · On 22.06.15 at 14:01, <[email protected]> wrote:

First of all, please Cc _all_ relevant maintainers.

Quoted 6 lines
xen/include/xen/bitops.h +0 -0
@@ -117,6 +117,14 @@ static inline int generic_fls64(__u64 x)
 # endif
 #endif
 
Quoted 11 lines

xen/include/xen/bitops.h static inline int generic_fls64(__u64 x) · line 126

Quoted 7 lines
xen/include/xen/bitops.h +7 -0
+static inline unsigned fls_long(unsigned long l)
+{
+    if (sizeof(l) == 4)
+        return fls(l);
+
+    return fls64(l);
+}

I'm not really opposed to this, but did you really verify that there's no suitable functionality in tree already (even if named differently)? I can't, e.g., see why flsl() wouldn't fit your needs.

#define fls64 flsl
So flsl also should be fine

Quoted 10 lines
Quoted 10 lines
xen/include/xen/log2.h new file +6 -0
@@ -0,0 +1,205 @@
+/*
+ * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
+ * Written by David Howells ([email protected])
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
Quoted 8 lines

xen/include/xen/log2.h line 12

Quoted 6 lines
xen/include/xen/log2.h new file +6 -0
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ */
+
+#ifndef _LINUX_LOG2_H
+#define _LINUX_LOG2_H

LINUX?

Most of the include/xen/*.h files has LINUX kept.
Anyway, I will remove it

Quoted 8 lines

xen/include/xen/log2.h line 21

Quoted 6 lines
xen/include/xen/log2.h new file +5 -0
+/*
+ * deal with unrepresentable constant logarithms
+ */
+extern __attribute__((const))
+int ____ilog2_NaN(void);

-ETOOMANYUNDERSCORES

I have taken this from linux. I have not introduced it.

Quoted 6 lines

xen/include/xen/log2.h line 18

Quoted 2 lines
xen/include/xen/log2.h new file +1 -0
+#if 1

??? (at least one more below)

Also, are you really needing all of what you add here?

I have taken complete log2 file from linux.

Regards
Vijay

#6 lore raw

Re: [RFC PATCH v3 02/18] xen: Add log2 functionality

Quoted 21 lines
Quoted 1 line
Quoted 1 line · On 24.06.15 at 15:05, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 22.06.15 at 14:01, <[email protected]> wrote:
xen/include/xen/bitops.h +0 -0
@@ -117,6 +117,14 @@ static inline int generic_fls64(__u64 x)
 # endif
 #endif
 
Quoted 11 lines

xen/include/xen/bitops.h static inline int generic_fls64(__u64 x) · line 126

Quoted 7 lines
xen/include/xen/bitops.h +7 -0
+static inline unsigned fls_long(unsigned long l)
+{
+    if (sizeof(l) == 4)
+        return fls(l);
+
+    return fls64(l);
+}

I'm not really opposed to this, but did you really verify that there's no suitable functionality in tree already (even if named differently)? I can't, e.g., see why flsl() wouldn't fit your needs.

Quoted 2 lines

#define fls64 flsl
So flsl also should be fine

No idea what you mean to say with the first of the two lines.

Quoted 11 lines
Quoted 7 lines

xen/include/xen/bitops.h static inline int generic_fls64(__u64 x)

Quoted 5 lines
xen/include/xen/bitops.h +5 -0
+/*
+ * deal with unrepresentable constant logarithms
+ */
+extern __attribute__((const))
+int ____ilog2_NaN(void);

-ETOOMANYUNDERSCORES

I have taken this from linux. I have not introduced it.

Quoted 1 line
Quoted 7 lines
Quoted 5 lines
Quoted 1 line

+#if 1

??? (at least one more below)

Also, are you really needing all of what you add here?

I have taken complete log2 file from linux.

And how much of it do you really need? In any event I'd like to ask not to copy rubbish like this. Also, in the description you say "ported" while taking the complete file should be expressed differently imo (and should indicate the version it was taken from so that people wanting to update it later will know what to diff against).

Jan

#7 lore raw

RFC PATCH v3 3/18 xen: console: Add ratelimit support for error message

In reply to [email protected]

From: Vijaya Kumar K <[email protected]>

XENLOG_ERR_RATE_LIMIT and XENLOG_G_ERR_RATE_LIMIT log levels are added to support rate limit for error messages

---
 xen/drivers/char/console.c |   17 +++++++++--------
 xen/include/xen/config.h   |    8 ++++++--
 2 files changed, 15 insertions(+), 10 deletions(-)
xen/drivers/char/console.c +9 -8
index fce4cc8..b01fde5 100644
@@ -92,15 +92,15 @@ static DEFINE_SPINLOCK(console_lock);
  * the lower threshold equal to the upper.
  */
 #ifdef NDEBUG
-#define XENLOG_UPPER_THRESHOLD       2 /* Do not print INFO and DEBUG  */
+#define XENLOG_UPPER_THRESHOLD       3 /* Do not print INFO and DEBUG  */
 #define XENLOG_LOWER_THRESHOLD       2 /* Always print ERR and WARNING */
-#define XENLOG_GUEST_UPPER_THRESHOLD 2 /* Do not print INFO and DEBUG  */
+#define XENLOG_GUEST_UPPER_THRESHOLD 3 /* Do not print INFO and DEBUG  */
 #define XENLOG_GUEST_LOWER_THRESHOLD 0 /* Rate-limit ERR and WARNING   */
 #else
-#define XENLOG_UPPER_THRESHOLD       4 /* Do not discard anything      */
-#define XENLOG_LOWER_THRESHOLD       4 /* Print everything             */
-#define XENLOG_GUEST_UPPER_THRESHOLD 4 /* Do not discard anything      */
-#define XENLOG_GUEST_LOWER_THRESHOLD 4 /* Print everything             */
+#define XENLOG_UPPER_THRESHOLD       5 /* Do not discard anything      */
+#define XENLOG_LOWER_THRESHOLD       5 /* Print everything             */
+#define XENLOG_GUEST_UPPER_THRESHOLD 5 /* Do not discard anything      */
+#define XENLOG_GUEST_LOWER_THRESHOLD 5 /* Print everything             */
 #endif
 /*
  * The XENLOG_DEFAULT is the default given to printks that
@@ -540,7 +540,7 @@ static int printk_prefix_check(char *p, char **pp)
             if ( loglvl == -1 )
                 loglvl = XENLOG_GUEST_DEFAULT;
             break;
-        case '0' ... '3':
+        case '0' ... '4':
             loglvl = p[1] - '0';
             break;
         }
@@ -554,7 +554,8 @@ static int printk_prefix_check(char *p, char **pp)
 
     return ((atomic_read(&print_everything) != 0) ||
             (loglvl < lower_thresh) ||
-            ((loglvl < upper_thresh) && printk_ratelimit()));
+            ((loglvl >= lower_thresh && loglvl < upper_thresh) &&
+             printk_ratelimit()));
 } 
 
 static void __init parse_console_timestamps(char *s)
xen/include/xen/config.h +6 -2
index f7258c7..b2ce0fe 100644
@@ -18,6 +18,8 @@
 /*
  * The following log levels are as follows:
  *
+ *   XENLOG_ERR_RATE_LIMIT: Rate limit error message from Xen and Guest
+ *
  *   XENLOG_ERR: Fatal errors, either Xen, Guest or Dom0
  *               is about to crash.
  *
@@ -41,11 +43,13 @@
  */
 #define XENLOG_ERR     "<0>"
 #define XENLOG_WARNING "<1>"
-#define XENLOG_INFO    "<2>"
-#define XENLOG_DEBUG   "<3>"
+#define XENLOG_ERR_RATE_LIMIT  "<2>"
+#define XENLOG_INFO    "<3>"
+#define XENLOG_DEBUG   "<4>"
 
 #define XENLOG_GUEST   "<G>"
 
+#define XENLOG_G_ERR_RATE_LIMIT     XENLOG_GUEST XENLOG_ERR_RATE_LIMIT
 #define XENLOG_G_ERR     XENLOG_GUEST XENLOG_ERR
 #define XENLOG_G_WARNING XENLOG_GUEST XENLOG_WARNING
 #define XENLOG_G_INFO    XENLOG_GUEST XENLOG_INFO
Signature · 1 line
1.7.9.5
#8 lore raw

Re: [RFC PATCH v3 03/18] xen: console: Add ratelimit support for error message

Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 22.06.15 at 14:01, <[email protected]> wrote:

From: Vijaya Kumar K <[email protected]>

XENLOG_ERR_RATE_LIMIT and XENLOG_G_ERR_RATE_LIMIT log levels are added to support rate limit for error messages

If you mean to say that rate limiting currently doesn't work for XENLOG_ERR messages, then that's a problem to be fixed by adjusting existing code, not by adding yet another log level.

Jan

#9 lore raw

Re: [RFC PATCH v3 03/18] xen: console: Add ratelimit support for error message

Quoted 9 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 22.06.15 at 14:01, <[email protected]> wrote:

From: Vijaya Kumar K <[email protected]>

XENLOG_ERR_RATE_LIMIT and XENLOG_G_ERR_RATE_LIMIT log levels are added to support rate limit for error messages

If you mean to say that rate limiting currently doesn't work for XENLOG_ERR messages, then that's a problem to be fixed by adjusting existing code, not by adding yet another log level.

For GUEST messages ERR and WARN are rate limited by setting lower threshold to 0 and upper threshold to 2 as below

#define XENLOG_GUEST_UPPER_THRESHOLD 2 /* Do not print INFO and DEBUG */
#define XENLOG_GUEST_LOWER_THRESHOLD 0 /* Rate-limit ERR and WARNING */

So do you recommend to set same threshold levels to Xen messages there by ERR & WARN are rate limited?

Regards
Vijay

#10 lore raw

Re: [RFC PATCH v3 03/18] xen: console: Add ratelimit support for error message

Quoted 9 lines · Vijay Kilari
Quoted 8 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 22.06.15 at 14:01, <[email protected]> wrote:

From: Vijaya Kumar K <[email protected]>

XENLOG_ERR_RATE_LIMIT and XENLOG_G_ERR_RATE_LIMIT log levels are added to support rate limit for error messages

If you mean to say that rate limiting currently doesn't work for XENLOG_ERR messages, then that's a problem to be fixed by adjusting existing code, not by adding yet another log level.

Quoted 8 lines

For GUEST messages ERR and WARN are rate limited by setting lower threshold to 0 and upper threshold to 2 as below

#define XENLOG_GUEST_UPPER_THRESHOLD 2 /* Do not print INFO and DEBUG */
#define XENLOG_GUEST_LOWER_THRESHOLD 0 /* Rate-limit ERR and WARNING */

So do you recommend to set same threshold levels to Xen messages there by ERR & WARN are rate limited?

Xen already has guest rate limiting logic, which has separate visible and ratelimited boundaries.

http://xenbits.xen.org/docs/unstable/misc/xen-command-line.html

Look at guest_loglvl.

If the existing logic isn't working then it should be fixed. We should not introduce a second way of doing this.

~Andrew

#11 lore raw

Re: [RFC PATCH v3 03/18] xen: console: Add ratelimit support for error message

In reply to Vijay Kilari
Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 25.06.15 at 15:14, <[email protected]> wrote:
Quoted 9 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 22.06.15 at 14:01, <[email protected]> wrote:

From: Vijaya Kumar K <[email protected]>

XENLOG_ERR_RATE_LIMIT and XENLOG_G_ERR_RATE_LIMIT log levels are added to support rate limit for error messages

If you mean to say that rate limiting currently doesn't work for XENLOG_ERR messages, then that's a problem to be fixed by adjusting existing code, not by adding yet another log level.

Quoted 8 lines

For GUEST messages ERR and WARN are rate limited by setting lower threshold to 0 and upper threshold to 2 as below

#define XENLOG_GUEST_UPPER_THRESHOLD 2 /* Do not print INFO and DEBUG */
#define XENLOG_GUEST_LOWER_THRESHOLD 0 /* Rate-limit ERR and WARNING */

So do you recommend to set same threshold levels to Xen messages there by ERR & WARN are rate limited?

I'm not sure I understand what you're asking: I recommend no change at all, unless you see something broken (in which case that's what I want clearly described).

Jan

#12 lore raw

RFC PATCH v3 4/18 xen/arm: gicv3: Refactor redistributor information

In reply to [email protected]

From: Vijaya Kumar K <[email protected]>

Separate redistributor information into rdist and rdist_prop structures.

The rdist_prop holds the redistributor common information and rdist holds the per cpu specific information.

This percpu rdist defined as global and shared with ITS driver

---
 xen/arch/arm/gic-v3.c             |   15 ++++++++++-----
 xen/include/asm-arm/gic_v3_defs.h |   15 +++++++++++++++
 2 files changed, 25 insertions(+), 5 deletions(-)
xen/arch/arm/gic-v3.c +10 -5
index 30682cf..b5c59f6 100644
@@ -53,6 +53,7 @@ static struct {
     paddr_t dbase;            /* Address of distributor registers */
     paddr_t dbase_size;
     void __iomem *map_dbase;  /* Mapped address of distributor registers */
+    struct rdist_prop rdist_data;
     struct rdist_region *rdist_regions;
     uint32_t  rdist_stride;
     unsigned int rdist_count; /* Number of rdist regions count */
@@ -63,10 +64,10 @@ static struct {
 static struct gic_info gicv3_info;
 
 /* per-cpu re-distributor base */
-static DEFINE_PER_CPU(void __iomem*, rbase);
+DEFINE_PER_CPU(struct rdist, rdist);
 
 #define GICD                   (gicv3.map_dbase)
-#define GICD_RDIST_BASE        (this_cpu(rbase))
+#define GICD_RDIST_BASE        (per_cpu(rdist, smp_processor_id()).rbase)
 #define GICD_RDIST_SGI_BASE    (GICD_RDIST_BASE + SZ_64K)
 
 /*
@@ -613,6 +614,7 @@ static int __init gicv3_populate_rdist(void)
     uint32_t aff;
     uint32_t reg;
     uint64_t typer;
+    uint64_t offset;
     uint64_t mpidr = cpu_logical_map(smp_processor_id());
 
     /*
@@ -648,9 +650,12 @@ static int __init gicv3_populate_rdist(void)
 
             if ( (typer >> 32) == aff )
             {
-                this_cpu(rbase) = ptr;
-                printk("GICv3: CPU%d: Found redistributor in region %d @%p\n",
-                        smp_processor_id(), i, ptr);
+                offset = ptr - gicv3.rdist_regions[i].map_base;
+                per_cpu(rdist, smp_processor_id()).rbase = ptr;
+                per_cpu(rdist, smp_processor_id()).phys_base =  gicv3.rdist_regions[i].base + offset;
+                printk("GICv3: CPU%d: Found redistributor in region %d @%"PRIpaddr"\n",
+                        smp_processor_id(), i,
+                        per_cpu(rdist, smp_processor_id()).phys_base);
                 return 0;
             }
             if ( gicv3.rdist_stride )
xen/include/asm-arm/gic_v3_defs.h +15 -0
index 556f114..acbb906 100644
@@ -152,6 +152,21 @@
 #define ICH_SGI_IRQ_SHIFT            24
 #define ICH_SGI_IRQ_MASK             0xf
 #define ICH_SGI_TARGETLIST_MASK      0xffff
+
+struct rdist {
+    void __iomem *rbase;
+    void * pend_page;
+    paddr_t phys_base;
+};
+
+struct rdist_prop {
+    void * prop_page;
+    int    id_bits;
+    uint64_t flags;
+};
+
+DECLARE_PER_CPU(struct rdist, rdist);
+
 #endif /* __ASM_ARM_GIC_V3_DEFS_H__ */
 
 /*
Signature · 1 line
1.7.9.5
#13 lore raw

Re: [RFC PATCH v3 04/18] xen/arm: gicv3: Refactor redistributor information

Hi,

Quoted 10 lines · On 22/06/15 13:01, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

Separate redistributor information into rdist and rdist_prop structures.

The rdist_prop holds the redistributor common information and rdist holds the per cpu specific information.

This percpu rdist defined as global and shared with ITS driver

This patch does more than refactoring, you are adding new fields which are not use here.

Please explain why you need them or move them in the patch where they are used. Comment on the usage of the field would be nice too.

FWIW, I would be in favor of the later.

Quoted 60 lines
---
 xen/arch/arm/gic-v3.c             |   15 ++++++++++-----
 xen/include/asm-arm/gic_v3_defs.h |   15 +++++++++++++++
 2 files changed, 25 insertions(+), 5 deletions(-)
xen/arch/arm/gic-v3.c +10 -5
index 30682cf..b5c59f6 100644
@@ -53,6 +53,7 @@ static struct {
     paddr_t dbase;            /* Address of distributor registers */
     paddr_t dbase_size;
     void __iomem *map_dbase;  /* Mapped address of distributor registers */
+    struct rdist_prop rdist_data;
     struct rdist_region *rdist_regions;
     uint32_t  rdist_stride;
     unsigned int rdist_count; /* Number of rdist regions count */
@@ -63,10 +64,10 @@ static struct {
 static struct gic_info gicv3_info;
 
 /* per-cpu re-distributor base */
-static DEFINE_PER_CPU(void __iomem*, rbase);
+DEFINE_PER_CPU(struct rdist, rdist);
 
 #define GICD                   (gicv3.map_dbase)
-#define GICD_RDIST_BASE        (this_cpu(rbase))
+#define GICD_RDIST_BASE        (per_cpu(rdist, smp_processor_id()).rbase)
 #define GICD_RDIST_SGI_BASE    (GICD_RDIST_BASE + SZ_64K)
 
 /*
@@ -613,6 +614,7 @@ static int __init gicv3_populate_rdist(void)
     uint32_t aff;
     uint32_t reg;
     uint64_t typer;
+    uint64_t offset;
     uint64_t mpidr = cpu_logical_map(smp_processor_id());
 
     /*
@@ -648,9 +650,12 @@ static int __init gicv3_populate_rdist(void)
 
             if ( (typer >> 32) == aff )
             {
-                this_cpu(rbase) = ptr;
-                printk("GICv3: CPU%d: Found redistributor in region %d @%p\n",
-                        smp_processor_id(), i, ptr);
+                offset = ptr - gicv3.rdist_regions[i].map_base;
+                per_cpu(rdist, smp_processor_id()).rbase = ptr;
+                per_cpu(rdist, smp_processor_id()).phys_base =  gicv3.rdist_regions[i].base + offset;
+                printk("GICv3: CPU%d: Found redistributor in region %d @%"PRIpaddr"\n",
+                        smp_processor_id(), i,
+                        per_cpu(rdist, smp_processor_id()).phys_base);
                 return 0;
             }
             if ( gicv3.rdist_stride )
xen/include/asm-arm/gic_v3_defs.h +0 -0
index 556f114..acbb906 100644

xen/include/asm-arm/gic_v3_defs.h line 158

Quoted 7 lines
xen/include/asm-arm/gic_v3_defs.h +4 -0
@@ -152,6 +152,21 @@
 #define ICH_SGI_IRQ_SHIFT            24
 #define ICH_SGI_IRQ_MASK             0xf
 #define ICH_SGI_TARGETLIST_MASK      0xffff
+
+struct rdist {
+    void __iomem *rbase;
+    void * pend_page;

void *pend_page;

xen/include/asm-arm/gic_v3_defs.h line 162

Quoted 4 lines
xen/include/asm-arm/gic_v3_defs.h +4 -0
+    paddr_t phys_base;
+};
+
+struct rdist_prop {

What does "prop" stand for?

xen/include/asm-arm/gic_v3_defs.h line 163

Quoted 1 line
xen/include/asm-arm/gic_v3_defs.h +1 -0
+    void * prop_page;

Ditto.

xen/include/asm-arm/gic_v3_defs.h line 172

Quoted 10 lines
xen/include/asm-arm/gic_v3_defs.h +6 -0
+    int    id_bits;
+    uint64_t flags;
+};
+
+DECLARE_PER_CPU(struct rdist, rdist);
+
 #endif /* __ASM_ARM_GIC_V3_DEFS_H__ */
 
 /*

Regards,

Signature · 1 line
Julien Grall
#14 lore raw

Re: [RFC PATCH v3 04/18] xen/arm: gicv3: Refactor redistributor information

In reply to [email protected]
Quoted 10 lines · On Mon, 2015-06-22 at 17:31 +0530, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

Separate redistributor information into rdist and rdist_prop structures.

The rdist_prop holds the redistributor common information and rdist holds the per cpu specific information.

This percpu rdist defined as global and shared with ITS driver

The vits or pits driver? The latter might be ok, the former would suggest some sort of layering violation I think.

Ian.

#15 lore raw

RFC PATCH v3 5/18 xen/arm: ITS: Port ITS driver to xen

In reply to [email protected]

From: Vijaya Kumar K <[email protected]>

Only required changes from Linux ITS driver is ported and compiled

---
v3:
- Only required changes from Linux ITS driver is ported
- Xen coding style is followed.

---
 xen/arch/arm/Makefile             |    1 +
 xen/arch/arm/gic-v3-its.c         | 1067 +++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h     |  214 ++++++++
 xen/include/asm-arm/gic.h         |    1 +
 xen/include/asm-arm/gic_v3_defs.h |  127 ++++-
 5 files changed, 1408 insertions(+), 2 deletions(-)
xen/arch/arm/Makefile +1 -0
index 935999e..1821ed2 100644
@@ -32,6 +32,7 @@ obj-y += shutdown.o
 obj-y += traps.o
 obj-y += vgic.o vgic-v2.o
 obj-$(CONFIG_ARM_64) += vgic-v3.o
+obj-$(CONFIG_ARM_64) += gic-v3-its.o
 obj-y += vtimer.o
 obj-y += vuart.o
 obj-y += hvm.o
xen/arch/arm/gic-v3-its.c new file +1067 -0

1,067 lines not on this page · raw message

xen/include/asm-arm/gic-its.h new file +214 -0

214 lines not on this page · raw message

xen/include/asm-arm/gic.h +1 -0
index 9e2acb7..e9d5f36 100644
@@ -161,6 +161,7 @@
     DT_MATCH_COMPATIBLE("arm,gic-400")
 
 #define DT_MATCH_GIC_V3 DT_MATCH_COMPATIBLE("arm,gic-v3")
+#define DT_MATCH_GIC_ITS DT_MATCH_COMPATIBLE("arm,gic-v3-its")
 
 /*
  * GICv3 registers that needs to be saved/restored
xen/include/asm-arm/gic_v3_defs.h +125 -2

159 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#16 lore raw

Re: [RFC PATCH v3 05/18] xen/arm: ITS: Port ITS driver to xen

Hi,

Quoted 4 lines · On 22/06/15 13:01, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

Only required changes from Linux ITS driver is ported and compiled

Can you list the changes you took from Linux? The coding style is not the same so it's hard to know what is the difference.

It would also have been nice to get the latest fix from Linux and say on which version you are based.

[..]

xen/arch/arm/gic-v3-its.c line 78

Quoted 9 lines
xen/arch/arm/gic-v3-its.c new file +9 -0
+/*
+ * The ITS structure - contains most of the infrastructure, with the
+ * msi_controller, the command queue, the collections, and the list of
+ * devices writing to it.
+ */
+struct its_node {
+    spinlock_t              lock;
+    struct list_head        entry;
+    void __iomem           *base;

The indentation seems wrong.

Quoted 9 lines
xen/arch/arm/gic-v3-its.c new file +9 -0
+    unsigned long           phys_base;
+    unsigned long           phys_size;
+    its_cmd_block           *cmd_base;
+    its_cmd_block           *cmd_write;
+    void                    *tables[GITS_BASER_NR_REGS];
+    struct its_collection   *collections;
+    u64                     flags;
+    u32                     ite_size;
+    struct dt_device_node   *dt_node;

xen/arch/arm/gic-v3-its.c line 97

Quoted 10 lines
xen/arch/arm/gic-v3-its.c new file +10 -0
+};
+
+#define ITS_ITT_ALIGN    SZ_256
+
+static LIST_HEAD(its_nodes);
+static DEFINE_SPINLOCK(its_lock);
+static struct rdist_prop  *gic_rdists;
+
+#define gic_data_rdist()            (per_cpu(rdist, smp_processor_id()))
+#define gic_data_rdist_rd_base()    (per_cpu(rdist, smp_processor_id()).rbase)

You could use gic_data_rdist().rbase

[..]

xen/arch/arm/gic-v3-its.c line 151

Quoted 12 lines
xen/arch/arm/gic-v3-its.c new file +12 -0
+#ifdef DEBUG_GIC_ITS
+void dump_cmd(its_cmd_block *cmd)
+{
+    printk("ITS: Phys_cmd CMD[0] = 0x%lx CMD[1] = 0x%lx CMD[2] = 0x%lx CMD[3] = 0x%lx\n",
+           cmd->raw_cmd[0], cmd->raw_cmd[1], cmd->raw_cmd[2], cmd->raw_cmd[3]);
+}
+#endif
+
+#define ITS_CMD_QUEUE_SZ            SZ_64K
+#define ITS_CMD_QUEUE_NR_ENTRIES    (ITS_CMD_QUEUE_SZ / sizeof(its_cmd_block))
+
+#define ALIGN(x, a) (((x) + (a - 1)) & ~(a - 1))

Please use ROUNDUP which does the same job.

xen/arch/arm/gic-v3-its.c line 162

Quoted 11 lines
xen/arch/arm/gic-v3-its.c new file +11 -0
+
+typedef struct its_collection *(*its_cmd_builder_t)(its_cmd_block *,
+                                                    struct its_cmd_desc *);
+
+static struct its_collection *its_build_mapd_cmd(its_cmd_block *cmd,
+                                                 struct its_cmd_desc *desc)
+{
+    unsigned long itt_addr;
+    u8 size;
+
+    size = max(order_base_2(desc->its_mapd_cmd.dev->nr_ites), 1);

Why did you replace the ilog2 by order_base_2?

xen/arch/arm/gic-v3-its.c line 166

Quoted 4 lines
xen/arch/arm/gic-v3-its.c new file +4 -0
+    itt_addr = __pa(desc->its_mapd_cmd.dev->itt_addr);
+    itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN);
+
+    memset(cmd, 0x0, sizeof(its_cmd_block));

You duplicate this line in ever build_*_cmd function. I think you should call only in one place before the builder.

xen/arch/arm/gic-v3-its.c line 170

Quoted 4 lines
xen/arch/arm/gic-v3-its.c new file +4 -0
+    cmd->mapd.cmd = GITS_CMD_MAPD;
+    cmd->mapd.devid = desc->its_mapd_cmd.dev->device_id;
+    cmd->mapd.size = size - 1;
+    cmd->mapd.itt = itt_addr >> 8;

I think the code is more difficult to read without the helpers. You opencode every trick in all the builder rather than in a single place.

xen/arch/arm/gic-v3-its.c line 175

Quoted 5 lines
xen/arch/arm/gic-v3-its.c new file +5 -0
+    cmd->mapd.valid =  desc->its_mapd_cmd.valid;
+
+#ifdef DEBUG_GIC_ITS
+    dump_cmd(cmd);
+#endif

The dump can be done in a single place rather than in every builder.

xen/arch/arm/gic-v3-its.c line 188

Quoted 12 lines
xen/arch/arm/gic-v3-its.c new file +12 -0
+    /* Take first collection for sync */
+    return &desc->its_mapd_cmd.dev->its->collections[0];
+}
+
+static struct its_collection *its_build_mapc_cmd(its_cmd_block *cmd,
+                                                 struct its_cmd_desc *desc)
+{
+    memset(cmd, 0x0, sizeof(its_cmd_block));
+    cmd->mapc.cmd = GITS_CMD_MAPC;
+    cmd->mapc.col = desc->its_mapc_cmd.col->col_id;
+    /*
+     * Thought target address field is only 32 bit.

I don't understand what you mean with "thought".

Quoted 1 line

+ * So take bit[48:16]

Can you explain why you are using only the [48:16]? I.e that target addresses must be 64KB aligned...

xen/arch/arm/gic-v3-its.c line 196

Quoted 7 lines
xen/arch/arm/gic-v3-its.c new file +7 -0
+     */
+    cmd->mapc.ta = desc->its_mapc_cmd.col->target_address >> 16;
+    cmd->mapc.valid = desc->its_mapc_cmd.valid;
+
+#ifdef DEBUG_GIC_ITS
+    dump_cmd(cmd);
+#endif

Missing newline.

[..]

xen/arch/arm/gic-v3-its.c line 368

Quoted 13 lines
xen/arch/arm/gic-v3-its.c new file +13 -0
+static void its_send_single_command(struct its_node *its,
+                                    its_cmd_builder_t builder,
+                                    struct its_cmd_desc *desc)
+{
+    its_cmd_block *cmd, *sync_cmd, *next_cmd;
+    struct its_collection *sync_col;
+    unsigned long flags;
+
+    spin_lock_irqsave(&its->lock, flags);
+
+    cmd = its_allocate_entry(its);
+    if ( !cmd )
+    {    /* We're soooooo screewed... */

I think the /* ... */ should go on a separate line.

Quoted 7 lines
xen/arch/arm/gic-v3-its.c new file +7 -0
+        its_err_ratelimit("ITS can't allocate, dropping command\n");
+        spin_unlock_irqrestore(&its->lock, flags);
+        return;
+    }
+    sync_col = builder(cmd, desc);
+    its_flush_cmd(its, cmd);
+

xen/arch/arm/gic-v3-its.c line 385

Quoted 10 lines
xen/arch/arm/gic-v3-its.c new file +10 -0
+    if ( sync_col )
+    {
+        sync_cmd = its_allocate_entry(its);
+        if ( !sync_cmd )
+        {
+            its_err_ratelimit("ITS can't SYNC, skipping\n");
+            goto post;
+        }
+        sync_cmd->sync.cmd = GITS_CMD_SYNC;
+        sync_cmd->sync.ta = sync_col->target_address >> 16;

This is the second place where you use target_address with ">> 16". May I ask why you dropped the helpers? At least it keeping such shift in a single place.

Also, given that all the usage of target_address is done with shift. Shouldn't you just do the shift once at setup?

[..]

xen/arch/arm/gic-v3-its.c line 557

Quoted 12 lines
xen/arch/arm/gic-v3-its.c new file +12 -0
+/*
+ * We allocate 64kB for PROPBASE. That gives us at most 64K LPIs to
+ * deal with (one configuration byte per interrupt). PENDBASE has to
+ * be 64kB aligned (one bit per LPI, plus 8192 bits for SPI/PPI/SGI).
+ */
+#define LPI_PROPBASE_SZ    SZ_64K
+#define LPI_PENDBASE_SZ    (LPI_PROPBASE_SZ / 8 + SZ_1K)
+
+/*
+ * This is how many bits of ID we need, including the useless ones.
+ */
+#define LPI_NRBITS    fls(LPI_PROPBASE_SZ + SZ_8K) - 1

Missing ( ... ) to ensure that LPI_NRBITS will be expanded correctly.

Although there is no need to switch to fls(...) you recently introduced ilog2.

Quoted 8 lines
xen/arch/arm/gic-v3-its.c new file +8 -0
+
+static int __init its_alloc_lpi_tables(void)
+{
+    paddr_t paddr;
+
+    gic_rdists->prop_page =
+           alloc_xenheap_pages(get_order_from_bytes(LPI_PROPBASE_SZ), 0);
+

xen/arch/arm/gic-v3-its.c line 575

Quoted 10 lines
xen/arch/arm/gic-v3-its.c new file +10 -0
+    if ( !gic_rdists->prop_page )
+    {
+        its_err("Failed to allocate PROPBASE\n");
+        return -ENOMEM;
+    }
+
+    paddr = __pa(gic_rdists->prop_page);
+    its_info("GIC: using LPI property table @%pa\n", &paddr);
+
+    /* Priority 0xa0, Group-1, disabled */

Well, you are not sure that GIC_PRI_IRQ is equal to 0xa0.

Quoted 20 lines
xen/arch/arm/gic-v3-its.c new file +20 -0
+    memset(gic_rdists->prop_page,
+           GIC_PRI_IRQ | LPI_PROP_GROUP1,
+           LPI_PROPBASE_SZ);
+
+    /* Make sure the GIC will observe the written configuration */
+    clean_and_invalidate_dcache_va_range(gic_rdists->prop_page,
+                                         LPI_PROPBASE_SZ);
+
+    return 0;
+}
+
+static const char *its_base_type_string[] = {
+    [GITS_BASER_TYPE_DEVICE]       = "Devices",
+    [GITS_BASER_TYPE_VCPU]         = "Virtual CPUs",
+    [GITS_BASER_TYPE_CPU]          = "Physical CPUs",
+    [GITS_BASER_TYPE_COLLECTION]   = "Interrupt Collections",
+    [GITS_BASER_TYPE_RESERVED5]    = "Reserved (5)",
+    [GITS_BASER_TYPE_RESERVED6]    = "Reserved (6)",
+    [GITS_BASER_TYPE_RESERVED7]    = "Reserved (7)",
+};

xen/arch/arm/gic-v3-its.c line 605

Quoted 10 lines
xen/arch/arm/gic-v3-its.c new file +10 -0
+
+static void its_free_tables(struct its_node *its)
+{
+    int i;
+
+    for ( i = 0; i < GITS_BASER_NR_REGS; i++ )
+    {
+        if ( its->tables[i] )
+        {
+            xfree(its->tables[i]);

As said on the previous version, the memory for the table is allocated via alloc_xenheap_pages. So freeing the memory should be done via free_xenheap_pages.

Quoted 13 lines
xen/arch/arm/gic-v3-its.c new file +13 -0
+            its->tables[i] = NULL;
+        }
+    }
+}
+
+static int its_alloc_tables(struct its_node *its)
+{
+    int err;
+    int i;
+    int psz = SZ_64K;
+    u64 shr = GITS_BASER_InnerShareable;
+
+    for ( i = 0; i < GITS_BASER_NR_REGS; i++ )

xen/arch/arm/gic-v3-its.c line 628

Quoted 10 lines
xen/arch/arm/gic-v3-its.c new file +10 -0
+    {
+        u64 val = readq_relaxed(its->base + GITS_BASER + i * 8);
+        u64 type = GITS_BASER_TYPE(val);
+        u64 entry_size = GITS_BASER_ENTRY_SIZE(val);
+        int order = get_order_from_bytes(psz);
+        int alloc_size;
+        u64 tmp;
+        void *base;
+
+        if (type == GITS_BASER_TYPE_NONE)

if ( ... )

Quoted 6 lines
xen/arch/arm/gic-v3-its.c new file +6 -0
+            continue;
+
+        /*
+         * Allocate as many entries as required to fit the
+         * range of device IDs that the ITS can grok... The ID
+         * space being incredibly sparse, this results in a

xen/arch/arm/gic-v3-its.c line 644

Quoted 10 lines
xen/arch/arm/gic-v3-its.c new file +10 -0
+         * massive waste of memory.
+         *
+         * For other tables, only allocate a single page.
+         */
+        if ( type == GITS_BASER_TYPE_DEVICE )
+        {
+            u64 typer = readq_relaxed(its->base + GITS_TYPER);
+            u32 ids = GITS_TYPER_DEVBITS(typer);
+
+            order = get_order_from_bytes((1UL << ids) * 8);

Why 8 and not entry_size?

Also, the latest Linux version is using max(...) to round up to the default page granularity if the size is smaller.

Quoted 12 lines
xen/arch/arm/gic-v3-its.c new file +12 -0
+            if (order >= MAX_ORDER)
+            {
+                order = MAX_ORDER - 1;
+                its_warn("Device Table too large, reduce its page order to %u\n",
+                     order);
+            }
+        }
+
+        alloc_size = (1 << order) * PAGE_SIZE;
+        base = alloc_xenheap_pages(order, 0);
+        if ( !base )
+        {

xen/arch/arm/gic-v3-its.c line 666

Quoted 10 lines
xen/arch/arm/gic-v3-its.c new file +10 -0
+            err = -ENOMEM;
+            goto out_free;
+        }
+        memset(base, 0, alloc_size);
+        its->tables[i] = base;
+retry_baser:
+        val = (__pa(base)                                        |
+               (type << GITS_BASER_TYPE_SHIFT)                   |
+               ((entry_size - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
+               GITS_BASER_WaWb                                   |

Same here, there was Linux changes to use non-cacheable where there is no shareability.

Quoted 6 lines
xen/arch/arm/gic-v3-its.c new file +6 -0
+               shr                                               |
+               GITS_BASER_VALID);
+
+        switch (psz) {
+        case SZ_4K:
+            val |= GITS_BASER_PAGE_SIZE_4K;

xen/arch/arm/gic-v3-its.c line 682

Quoted 10 lines
xen/arch/arm/gic-v3-its.c new file +10 -0
+            break;
+        case SZ_16K:
+            val |= GITS_BASER_PAGE_SIZE_16K;
+            break;
+        case SZ_64K:
+            val |= GITS_BASER_PAGE_SIZE_64K;
+            break;
+        }
+
+        val |= (((alloc_size / psz) - 1) & 0xffUL);

I don't think the & 0xffUL is useful. If alloc_size is too big it would result to an invalid val anyway.

FWIW, Linux doesn't have it.

Quoted 24 lines
xen/arch/arm/gic-v3-its.c new file +24 -0
+
+        writeq_relaxed(val, its->base + GITS_BASER + i * 8);
+        tmp = readq_relaxed(its->base + GITS_BASER + i * 8);
+
+        if ( (val ^ tmp) & GITS_BASER_SHAREABILITY_MASK )
+        {
+            /*
+             * Shareability didn't stick. Just use
+             * whatever the read reported, which is likely
+             * to be the only thing this redistributor
+             * supports.
+             */
+            shr = tmp & GITS_BASER_SHAREABILITY_MASK;
+            goto retry_baser;
+        }
+
+        if ( (val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK )
+        {
+            /*
+             * Page size didn't stick. Let's try a smaller
+             * size and retry. If we reach 4K, then
+             * something is horribly wrong...
+             */
+            switch (psz) {

xen/arch/arm/gic-v3-its.c line 716

Quoted 10 lines
xen/arch/arm/gic-v3-its.c new file +10 -0
+            case SZ_16K:
+                psz = SZ_4K;
+                goto retry_baser;
+            case SZ_64K:
+                psz = SZ_16K;
+                goto retry_baser;
+            }
+        }
+
+        /* skip comparin cacheability fields as they are implementation

/*
* Fooo
*/
And s/comparin/comparing/

xen/arch/arm/gic-v3-its.c line 720

Quoted 4 lines
xen/arch/arm/gic-v3-its.c new file +4 -0
+         * defined.
+         */
+        val = val << 5;
+        tmp = tmp << 5;

Why do you need a such trick? The whole purpose of the below check is to verify that the value is correctly taken by the ITS. As the field should be RW, we would be in big trouble if the cache is not taken into account by the ITS.

FWIW, Linux doesn't do it.

xen/arch/arm/gic-v3-its.c line 725

Quoted 4 lines
xen/arch/arm/gic-v3-its.c new file +4 -0
+        if ( val != tmp )
+        {
+            its_err("ITS: GITS_BASER%d doesn't stick: %lx %lx\n",
+                    i, (unsigned long) val, (unsigned long) tmp);

You won't print a correct value here with your "val <<".

[..]

Quoted 6 lines
xen/arch/arm/gic-v3-its.c new file +6 -0
+    /*
+     * Make sure any change to the table is observable by the GIC.
+     */
+    dsb(sy);
+
+    /* set PROPBASE */

xen/arch/arm/gic-v3-its.c line 811

Quoted 10 lines
xen/arch/arm/gic-v3-its.c new file +10 -0
+    val = (__pa(gic_rdists->prop_page)   |
+           GICR_PROPBASER_InnerShareable |
+           GICR_PROPBASER_WaWb           |
+           ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
+
+    writeq_relaxed(val, rbase + GICR_PROPBASER);
+    tmp = readq_relaxed(rbase + GICR_PROPBASER);
+
+    if ( (tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK )
+    {

If there is no shareability, Linux is dropping the cacheability.

xen/arch/arm/gic-v3-its.c line 822

Quoted 11 lines
xen/arch/arm/gic-v3-its.c new file +11 -0
+        its_info("GIC: using cache flushing for LPI property table\n");
+        gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
+    }
+
+    /* set PENDBASE */
+    val = (__pa(pend_page)               |
+           GICR_PROPBASER_InnerShareable |
+           GICR_PROPBASER_WaWb);
+
+    writeq_relaxed(val, rbase + GICR_PENDBASER);
+

Ditto

[..]

Quoted 20 lines
xen/arch/arm/gic-v3-its.c new file +20 -0
+static void its_cpu_init_collection(void)
+{
+    struct its_node *its;
+    int cpu;
+
+    spin_lock(&its_lock);
+    cpu = smp_processor_id();
+
+    list_for_each_entry(its, &its_nodes, entry)
+    {
+        u64 target;
+        /*
+         * We now have to bind each collection to its target
+         * redistributor.
+         */
+        if ( readq_relaxed(its->base + GITS_TYPER) & GITS_TYPER_PTA )
+        {
+            /*
+             * This ITS wants the physical address of the
+             * redistributor.

xen/arch/arm/gic-v3-its.c line 861

Quoted 10 lines
xen/arch/arm/gic-v3-its.c new file +10 -0
+             */
+            target = gic_data_rdist().phys_base;
+        }
+        else
+        {
+            /*
+             * This ITS wants a linear CPU number.
+             */
+            target = readq_relaxed(gic_data_rdist_rd_base() + GICR_TYPER);
+            target = GICR_TYPER_CPU_NUMBER(target);

Why did you drop the << 16?

Although, as said earlier, given the usage of target_address you could do shift >> directly in this function rather than on multiple__ place.

[..]

Quoted 3 lines
xen/arch/arm/gic-v3-its.c new file +3 -0
+
+static int its_probe(struct dt_device_node *node)
+{

[..]

xen/arch/arm/gic-v3-its.c line 984

Quoted 12 lines
xen/arch/arm/gic-v3-its.c new file +12 -0
+    err = its_alloc_collections(its);
+    if ( err )
+        goto out_free_tables;
+
+    baser = (__pa(its->cmd_base)            |
+             GITS_CBASER_WaWb               |
+             GITS_CBASER_InnerShareable     |
+             (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
+             GITS_CBASER_VALID);
+
+    writeq_relaxed(baser, its->base + GITS_CBASER);
+    tmp = readq_relaxed(its->base + GITS_CBASER);

Missing checking the shareability.

xen/arch/arm/gic-v3-its.c line 992

Quoted 8 lines
xen/arch/arm/gic-v3-its.c new file +8 -0
+    writeq_relaxed(0, its->base + GITS_CWRITER);
+    writel_relaxed(GITS_CTLR_ENABLE, its->base + GITS_CTLR);
+
+    if ( (tmp ^ baser) & GITS_BASER_SHAREABILITY_MASK )
+    {
+        its_info("ITS: using cache flushing for cmd queue\n");
+        its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
+    }

Technically you should check that the ITS node contains a property "msi-controller".

[..]

xen/arch/arm/gic-v3-its.c line 1036

Quoted 5 lines
xen/arch/arm/gic-v3-its.c new file +5 -0
+int its_init(struct rdist_prop *rdists)
+{
+    struct dt_device_node *np = NULL;
+
+    static const struct dt_device_match its_device_ids[] __initconst =

If the structure is __initconst, the function should be __init too.

Quoted 9 lines
xen/arch/arm/gic-v3-its.c new file +9 -0
+    {
+        DT_MATCH_GIC_ITS,
+        { /* sentinel */ },
+    };
+
+    for (np = dt_find_matching_node(NULL, its_device_ids); np;
+             np = dt_find_matching_node(np, its_device_ids))
+        its_probe(np);
+

xen/arch/arm/gic-v3-its.c line 1055

Quoted 10 lines
xen/arch/arm/gic-v3-its.c new file +10 -0
+    if ( list_empty(&its_nodes) )
+    {
+        its_warn("ITS: No ITS available, not enabling LPIs\n");
+        return -ENXIO;
+    }
+
+    gic_rdists = rdists;
+    its_alloc_lpi_tables();
+
+    BUILD_BUG_ON(sizeof(its_cmd_block) != 32);

Why this build bug on here? Shouldn't it be part of the builder code?

[..]

Quoted 21 lines
xen/include/asm-arm/gic-its.h new file +15 -0
new file mode 100644
index 0000000..4e42f7f
@@ -0,0 +1,214 @@
+/*
+ * Copyright (C) 2015 Cavium Inc.
+ * Vijaya Kumar K <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.

xen/include/asm-arm/gic-its.h line 25

Quoted 10 lines
xen/include/asm-arm/gic-its.h new file +10 -0
+ */
+
+#ifndef __ASM_ARM_GIC_ITS_H__
+#define __ASM_ARM_GIC_ITS_H__
+
+#include <asm/gic_v3_defs.h>
+
+/*
+ * Collection structure - just an ID, and a redistributor address to
+ * ping. We use one per CPU as a bag of interrupts assigned to this

s/bag/collection/

Quoted 8 lines
xen/include/asm-arm/gic-its.h new file +8 -0
+ * CPU.
+ */
+struct its_collection {
+    u64 target_address;
+    u16 col_id;
+};
+
+/* ITS command structures */

xen/include/asm-arm/gic-its.h line 43

Quoted 10 lines
xen/include/asm-arm/gic-its.h new file +10 -0
+typedef struct __packed {
+    u64 cmd:8;
+    u64 res1:56;
+    u64 res2:64;
+    u64 col:16;
+    u64 ta:32;
+    u64 res3:15;
+    u64 valid:1;
+    u64 res4:64;
+}mapc_cmd_t;

Missing space

xen/include/asm-arm/gic-its.h line 56

Quoted 13 lines
xen/include/asm-arm/gic-its.h new file +13 -0
+
+typedef struct __packed {
+    u64 cmd:8;
+    u64 res1:24;
+    u64 devid:32;
+    u64 size:5;
+    u64 res2:59;
+    u64 res3:8;
+    u64 itt:40;
+    u64 res4:15;
+    u64 valid:1;
+    u64 res5:64;
+}mapd_cmd_t;

ditto

xen/include/asm-arm/gic-its.h line 68

Quoted 12 lines
xen/include/asm-arm/gic-its.h new file +12 -0
+
+typedef struct __packed {
+    u64 cmd:8;
+    u64 res1:24;
+    u64 devid:32;
+    u64 event:32;
+    u64 res2:32;
+    u64 col:16;
+    u64 res3:48;
+    u64 res4:64;
+}mapi_cmd_t;
+

ditto

xen/include/asm-arm/gic-its.h line 79

Quoted 11 lines
xen/include/asm-arm/gic-its.h new file +11 -0
+typedef struct __packed {
+    u64 cmd:8;
+    u64 res1:24;
+    u64 devid:32;
+    u64 event:32;
+    u64 phy_id:32;
+    u64 col:16;
+    u64 res3:48;
+    u64 res4:64;
+}mapvi_cmd_t;
+

ditto

xen/include/asm-arm/gic-its.h line 89

Quoted 10 lines
xen/include/asm-arm/gic-its.h new file +10 -0
+typedef struct __packed {
+    u64 cmd:8;
+    u64 res1:24;
+    u64 devid:32;
+    u64 event:32;
+    u64 res2:32;
+    u64 col:16;
+    u64 res3:48;
+    u64 res4:64;
+}movi_cmd_t;

ditto

xen/include/asm-arm/gic-its.h line 99

Quoted 10 lines
xen/include/asm-arm/gic-its.h new file +10 -0
+
+typedef struct __packed {
+    u64 cmd:8;
+    u64 res1:24;
+    u64 devid:32;
+    u64 event:32;
+    u64 res2:32;
+    u64 res3:64;
+    u64 res4:64;
+}discard_cmd_t;

ditto

xen/include/asm-arm/gic-its.h line 109

Quoted 10 lines
xen/include/asm-arm/gic-its.h new file +10 -0
+
+typedef struct __packed {
+    u64 cmd:8;
+    u64 res1:24;
+    u64 devid:32;
+    u64 event:32;
+    u64 res2:32;
+    u64 res3:64;
+    u64 res4:64;
+}inv_cmd_t;

ditto

xen/include/asm-arm/gic-its.h line 121

Quoted 12 lines
xen/include/asm-arm/gic-its.h new file +12 -0
+
+typedef struct __packed {
+    u64 cmd:8;
+    u64 res1:56;
+    u64 res2:64;
+    u64 res3:16;
+    u64 ta1:32;
+    u64 res4:16;
+    u64 res5:16;
+    u64 ta2:32;
+    u64 res6:16;
+}movall_cmd_t;

ditto

xen/include/asm-arm/gic-its.h line 130

Quoted 9 lines
xen/include/asm-arm/gic-its.h new file +9 -0
+
+typedef struct __packed {
+    u64 cmd:8;
+    u64 res1:56;
+    u64 res2:64;
+    u64 col:16;
+    u64 res3:48;
+    u64 res4:64;
+}invall_cmd_t;

ditto

xen/include/asm-arm/gic-its.h line 140

Quoted 10 lines
xen/include/asm-arm/gic-its.h new file +10 -0
+
+typedef struct __packed {
+    u64 cmd:8;
+    u64 res1:24;
+    u64 devid:32;
+    u64 event:32;
+    u64 res2:32;
+    u64 res3:64;
+    u64 res4:64;
+}int_cmd_t;

ditto

xen/include/asm-arm/gic-its.h line 150

Quoted 10 lines
xen/include/asm-arm/gic-its.h new file +10 -0
+
+typedef struct __packed {
+    u64 cmd:8;
+    u64 res1:24;
+    u64 devid:32;
+    u64 event:32;
+    u64 res2:32;
+    u64 res3:64;
+    u64 res4:64;
+}clear_cmd_t;

ditto

xen/include/asm-arm/gic-its.h line 160

Quoted 10 lines
xen/include/asm-arm/gic-its.h new file +10 -0
+
+typedef struct __packed {
+    u64 cmd:8;
+    u64 res1:56;
+    u64 res2:64;
+    u64 res3:16;
+    u64 ta:32;
+    u64 res4:16;
+    u64 res5:64;
+}sync_cmd_t;

ditto

Quoted 6 lines
xen/include/asm-arm/gic-its.h new file +6 -0
+
+typedef union {
+    u64           raw_cmd[4];
+    mapc_cmd_t    mapc;
+    mapd_cmd_t    mapd;
+    mapi_cmd_t    mapi;

xen/include/asm-arm/gic-its.h line 176

Quoted 10 lines
xen/include/asm-arm/gic-its.h new file +10 -0
+    mapvi_cmd_t   mapvi;
+    movi_cmd_t    movi;
+    movall_cmd_t  movall;
+    inv_cmd_t     inv;
+    invall_cmd_t  invall;
+    discard_cmd_t discard;
+    int_cmd_t     int_cmd;
+    clear_cmd_t   clear;
+    sync_cmd_t    sync;
+}its_cmd_block;

ditto

Quoted 17 lines
xen/include/asm-arm/gic-its.h new file +17 -0
+/*
+ * The ITS view of a device - belongs to an ITS, a collection, owns an
+ * interrupt translation table, and a list of interrupts.
+ */
+struct its_device {
+    /* Physical ITS */
+    struct its_node         *its;
+    /* Device ITT address */
+    paddr_t                 itt_addr;
+    /* Device ITT size */
+    unsigned long           itt_size;
+    /* Physical LPI map */
+    unsigned long           *lpi_map;
+    /* First Physical LPI number assigned */
+    u32                     lpi_base;
+    /* Number of Physical LPIs assigned */
+    int                     nr_lpis;

xen/include/asm-arm/gic-its.h line 203

Quoted 10 lines
xen/include/asm-arm/gic-its.h new file +10 -0
+    /* Number of ITES entries */
+    u32                     nr_ites;
+    /* Physical Device id */
+    u32                     device_id;
+};
+
+static inline uint8_t its_decode_cmd(its_cmd_block *cmd)
+{
+    return cmd->raw_cmd[0] & 0xff;
+}

Please defined any exported function of gic-v3-its.c here and not in patches where they are used.

[..]

Quoted 17 lines
xen/include/asm-arm/gic.h +1 -0
index 9e2acb7..e9d5f36 100644
@@ -161,6 +161,7 @@
     DT_MATCH_COMPATIBLE("arm,gic-400")
 
 #define DT_MATCH_GIC_V3 DT_MATCH_COMPATIBLE("arm,gic-v3")
+#define DT_MATCH_GIC_ITS DT_MATCH_COMPATIBLE("arm,gic-v3-its")
 
 /*
  * GICv3 registers that needs to be saved/restored
xen/include/asm-arm/gic_v3_defs.h +0 -0
index acbb906..dc4fe14 100644

xen/include/asm-arm/gic_v3_defs.h line 48

Quoted 4 lines
xen/include/asm-arm/gic_v3_defs.h +1 -0
@@ -45,9 +45,11 @@
 #define GICC_SRE_EL2_DIB             (1UL << 2)
 #define GICC_SRE_EL2_ENEL1           (1UL << 3)
 
+#define GICR_CTL_ENABLE              (1U << 0)

Why? You've defined GICR_CTLR_ENABLE_LPIS below and don't use it.

Regards,

Signature · 1 line
Julien Grall
#17 lore raw

Re: [RFC PATCH v3 05/18] xen/arm: ITS: Port ITS driver to xen

Hi Julien,

Quoted 9 lines · Julien Grall

Hi,

xen/arch/arm/gic-v3-its.c line 170

Quoted 4 lines
xen/arch/arm/gic-v3-its.c new file +4 -0
+    cmd->mapd.cmd = GITS_CMD_MAPD;
+    cmd->mapd.devid = desc->its_mapd_cmd.dev->device_id;
+    cmd->mapd.size = size - 1;
+    cmd->mapd.itt = itt_addr >> 8;

I think the code is more difficult to read without the helpers. You opencode every trick in all the builder rather than in a single place.

ITS commands does not consider all the bits of the values esp. when
encoding addresses. So we need to shift and write it. Helpers might be useful, but usage is hardly once or twice in the code.

Quoted 33 lines
Quoted 20 lines
xen/arch/arm/gic-v3-its.c new file +20 -0
+static void its_cpu_init_collection(void)
+{
+    struct its_node *its;
+    int cpu;
+
+    spin_lock(&its_lock);
+    cpu = smp_processor_id();
+
+    list_for_each_entry(its, &its_nodes, entry)
+    {
+        u64 target;
+        /*
+         * We now have to bind each collection to its target
+         * redistributor.
+         */
+        if ( readq_relaxed(its->base + GITS_TYPER) & GITS_TYPER_PTA )
+        {
+            /*
+             * This ITS wants the physical address of the
+             * redistributor.

xen/arch/arm/gic-v3-its.c line 861

Quoted 10 lines
xen/arch/arm/gic-v3-its.c new file +10 -0
+             */
+            target = gic_data_rdist().phys_base;
+        }
+        else
+        {
+            /*
+             * This ITS wants a linear CPU number.
+             */
+            target = readq_relaxed(gic_data_rdist_rd_base() + GICR_TYPER);
+            target = GICR_TYPER_CPU_NUMBER(target);

Why did you drop the << 16?

Quoted 2 lines

Although, as said earlier, given the usage of target_address you could do shift >> directly in this function rather than on multiple__ place.

Yes, we can shift at the setup. But we lose actual value of target_address. Also we shift because ITS command skips lower 16 bits

Quoted 21 lines

xen/arch/arm/gic-v3-its.c line 1036

Quoted 5 lines
xen/arch/arm/gic-v3-its.c new file +5 -0
+int its_init(struct rdist_prop *rdists)
+{
+    struct dt_device_node *np = NULL;
+
+    static const struct dt_device_match its_device_ids[] __initconst =

If the structure is __initconst, the function should be __init too.

Quoted 13 lines
xen/arch/arm/gic-v3-its.c new file +13 -0
+    {
+        DT_MATCH_GIC_ITS,
+        { /* sentinel */ },
+    };
+
+    for (np = dt_find_matching_node(NULL, its_device_ids); np;
+             np = dt_find_matching_node(np, its_device_ids))
+        its_probe(np);
+
+    if ( list_empty(&its_nodes) )
+    {
+        its_warn("ITS: No ITS available, not enabling LPIs\n");
+        return -ENXIO;
Quoted 8 lines

xen/arch/arm/gic-v3-its.c line 1055

Quoted 6 lines
xen/arch/arm/gic-v3-its.c new file +6 -0
+    }
+
+    gic_rdists = rdists;
+    its_alloc_lpi_tables();
+
+    BUILD_BUG_ON(sizeof(its_cmd_block) != 32);

Why this build bug on here? Shouldn't it be part of the builder code?

Where is builder code in xen?

#18 lore raw

Re: [RFC PATCH v3 05/18] xen/arm: ITS: Port ITS driver to xen

Quoted 1 line · Vijay Kilari

Hi Julien,

Hi Vijay,

Quoted 8 lines · Julien Grall
Quoted 7 lines

Hi,

Quoted 4 lines
xen/arch/arm/gic-v3-its.c new file +4 -0
+    cmd->mapd.cmd = GITS_CMD_MAPD;
+    cmd->mapd.devid = desc->its_mapd_cmd.dev->device_id;
+    cmd->mapd.size = size - 1;
+    cmd->mapd.itt = itt_addr >> 8;
Quoted 5 lines
Quoted 2 lines

I think the code is more difficult to read without the helpers. You opencode every trick in all the builder rather than in a single place.

ITS commands does not consider all the bits of the values esp. when
encoding addresses. So we need to shift and write it.

A comment on the code would have been welcomed (either here or in the definition of the structure). By naming the field itt it gives the impression that we have to pass all the ITT address.

Quoted 1 line

Helpers might be useful, but usage is hardly once or twice in the code.

IHMO, once is okay but twice is too much. It means that you duplicated code that may be non straigh-forward to understand (such as shift). Anyway, I will let Ian & Stefano decide.

Quoted 34 lines
Quoted 34 lines
Quoted 21 lines
xen/arch/arm/gic-v3-its.c new file +20 -0
+static void its_cpu_init_collection(void)
+{
+    struct its_node *its;
+    int cpu;
+
+    spin_lock(&its_lock);
+    cpu = smp_processor_id();
+
+    list_for_each_entry(its, &its_nodes, entry)
+    {
+        u64 target;
+        /*
+         * We now have to bind each collection to its target
+         * redistributor.
+         */
+        if ( readq_relaxed(its->base + GITS_TYPER) & GITS_TYPER_PTA )
+        {
+            /*
+             * This ITS wants the physical address of the
+             * redistributor.

xen/arch/arm/gic-v3-its.c line 861

Quoted 10 lines
xen/arch/arm/gic-v3-its.c new file +10 -0
+             */
+            target = gic_data_rdist().phys_base;
+        }
+        else
+        {
+            /*
+             * This ITS wants a linear CPU number.
+             */
+            target = readq_relaxed(gic_data_rdist_rd_base() + GICR_TYPER);
+            target = GICR_TYPER_CPU_NUMBER(target);

Why did you drop the << 16?

Quoted 4 lines
Quoted 2 lines

Although, as said earlier, given the usage of target_address you could do shift >> directly in this function rather than on multiple__ place.

Yes, we can shift at the setup. But we lose actual value of target_address.

Do we really need to get the target_address intact? I believe not.

Quoted 8 lines
Quoted 6 lines

xen/arch/arm/gic-v3-its.c line 1055

Quoted 4 lines
xen/arch/arm/gic-v3-its.c new file +4 -0
+    gic_rdists = rdists;
+    its_alloc_lpi_tables();
+
+    BUILD_BUG_ON(sizeof(its_cmd_block) != 32);

Why this build bug on here? Shouldn't it be part of the builder code?

Where is builder code in xen?

By builder I meant the function who built the command i.e its_send_single_command.

Regards,

Signature · 1 line
Julien Grall
#19 lore raw

Re: [RFC PATCH v3 05/18] xen/arm: ITS: Port ITS driver to xen

In reply to [email protected]

On Mon, 2015-06-22 at 17:31 +0530, [email protected] wrote:
[...]

Quoted 11 lines
xen/arch/arm/gic-v3-its.c new file +11 -0
+/*
+ * ITS command descriptors - parameters to be encoded in a command
+ * block.
+ */
+struct its_cmd_desc {
+    union {
+        struct {
+            struct its_collection *col;
+            u32 event_id;
+            u32 dev_id;
+        } its_inv_cmd;

[...]

Quoted 14 lines
xen/arch/arm/gic-v3-its.c new file +14 -0
+static struct its_collection *its_build_inv_cmd(its_cmd_block *cmd,
+                                                struct its_cmd_desc *desc)
+{
+    memset(cmd, 0x0, sizeof(its_cmd_block));
+    cmd->inv.cmd = GITS_CMD_INV;
+    cmd->inv.devid = desc->its_inv_cmd.dev_id;
+    cmd->inv.event = desc->its_inv_cmd.event_id;
+
+#ifdef DEBUG_GIC_ITS
+    dump_cmd(cmd);
+#endif
+
+    return desc->its_inv_cmd.col;
+}

[...]

Quoted 11 lines
xen/arch/arm/gic-v3-its.c new file +11 -0
+void its_send_inv(struct its_device *dev, struct its_collection *col,
+                  u32 event_id)
+{
+    struct its_cmd_desc desc;
+
+    desc.its_inv_cmd.dev_id = dev->device_id;
+    desc.its_inv_cmd.event_id = event_id;
+    desc.its_inv_cmd.col = col;
+
+    its_send_single_command(dev->its, its_build_inv_cmd, &desc);
+}

[...]

xen/include/asm-arm/gic-its.h line 109

Quoted 9 lines
xen/include/asm-arm/gic-its.h new file +9 -0
+typedef struct __packed {
+    u64 cmd:8;
+    u64 res1:24;
+    u64 devid:32;
+    u64 event:32;
+    u64 res2:32;
+    u64 res3:64;
+    u64 res4:64;
+}inv_cmd_t;

(I've trimmed this to just the INV command, but it's the same for all of them)

I suppose this is a mix of the way the Linux code was structured and my request to use a struct/union to encode these things, but I'm afraid this is not how I intended to suggest things be done.

What I expected was something analogous to the hsr or lpae_t types, e.g. a single:

union its_cmd {
    uint64_t bits[N];

    struct {
        uint8_t cmd;
        uint8_t pad[...];
    } hdr;

    struct {
        uint8_t cmd;
        uint8_t res1[3];
        uint32_t devid;
        uint32_t event;
        uint64_t res2[2];
    } inv;
};

So its_send_single_command can take a "union its_cmd *" and its_send_inv should look like:

void its_send_inv(struct its_device *dev, struct its_collection *col,

                  u32 event_id)
{
    union its_cmd cmd;
    /* memset perhaps, or sets .bits = {0,} */

    cmd.inv.cmd = GITS_CMD_INV;
    cmd.inv.dev_id = dev->device_id;
    cmd.inv.event_id = event_id;
    cmd.inv.col = col;

    return its_send_single_command(dev->its, &cmd);
}

I've omitted the its_ prefix and _cmd/_desc suffix where they aren't needed in the context they are used. (so not cmd.cmd_inv etc).

I've also used proper types where possible instead of bitfields of u64 (although unsigned long bitfields should still be used for sub 8-bit fields).

The "hdr" member of the union should contain any field which is global to all commands and which generic code (i.e. which isn't aware which command is in the cmd in its hand) can use. Off the top of my head that is just the cmd code itself.

You should also consider doing the collection sync in the caller as appropriate instead of pushing it down into its_send_single_command. IMHO its_send_single_command should do just that, not optionally do some other command too.

Ian.

#20 lore raw

Re: [RFC PATCH v3 05/18] xen/arm: ITS: Port ITS driver to xen

Quoted 60 lines · Ian Campbell

On Mon, 2015-06-22 at 17:31 +0530, [email protected] wrote:
[...]

Quoted 11 lines
xen/arch/arm/gic-v3-its.c new file +11 -0
+/*
+ * ITS command descriptors - parameters to be encoded in a command
+ * block.
+ */
+struct its_cmd_desc {
+    union {
+        struct {
+            struct its_collection *col;
+            u32 event_id;
+            u32 dev_id;
+        } its_inv_cmd;

[...]

Quoted 14 lines
xen/arch/arm/gic-v3-its.c new file +14 -0
+static struct its_collection *its_build_inv_cmd(its_cmd_block *cmd,
+                                                struct its_cmd_desc *desc)
+{
+    memset(cmd, 0x0, sizeof(its_cmd_block));
+    cmd->inv.cmd = GITS_CMD_INV;
+    cmd->inv.devid = desc->its_inv_cmd.dev_id;
+    cmd->inv.event = desc->its_inv_cmd.event_id;
+
+#ifdef DEBUG_GIC_ITS
+    dump_cmd(cmd);
+#endif
+
+    return desc->its_inv_cmd.col;
+}

[...]

Quoted 11 lines
xen/arch/arm/gic-v3-its.c new file +11 -0
+void its_send_inv(struct its_device *dev, struct its_collection *col,
+                  u32 event_id)
+{
+    struct its_cmd_desc desc;
+
+    desc.its_inv_cmd.dev_id = dev->device_id;
+    desc.its_inv_cmd.event_id = event_id;
+    desc.its_inv_cmd.col = col;
+
+    its_send_single_command(dev->its, its_build_inv_cmd, &desc);
+}

[...]

xen/include/asm-arm/gic-its.h line 109

Quoted 9 lines
xen/include/asm-arm/gic-its.h new file +9 -0
+typedef struct __packed {
+    u64 cmd:8;
+    u64 res1:24;
+    u64 devid:32;
+    u64 event:32;
+    u64 res2:32;
+    u64 res3:64;
+    u64 res4:64;
+}inv_cmd_t;

(I've trimmed this to just the INV command, but it's the same for all of them)

I suppose this is a mix of the way the Linux code was structured and my request to use a struct/union to encode these things, but I'm afraid this is not how I intended to suggest things be done.

What I expected was something analogous to the hsr or lpae_t types, e.g. a single:

Quoted 16 lines
union its_cmd {
    uint64_t bits[N];

    struct {
        uint8_t cmd;
        uint8_t pad[...];
    } hdr;

    struct {
        uint8_t cmd;
        uint8_t res1[3];
        uint32_t devid;
        uint32_t event;
        uint64_t res2[2];
    } inv;
};

Commands like MAPD

Quoted 37 lines

So its_send_single_command can take a "union its_cmd *" and its_send_inv should look like:

void its_send_inv(struct its_device *dev, struct its_collection *col,

                  u32 event_id)
{
    union its_cmd cmd;
    /* memset perhaps, or sets .bits = {0,} */

    cmd.inv.cmd = GITS_CMD_INV;
    cmd.inv.dev_id = dev->device_id;
    cmd.inv.event_id = event_id;
    cmd.inv.col = col;

    return its_send_single_command(dev->its, &cmd);
}

I've omitted the its_ prefix and _cmd/_desc suffix where they aren't needed in the context they are used. (so not cmd.cmd_inv etc).

I've also used proper types where possible instead of bitfields of u64 (although unsigned long bitfields should still be used for sub 8-bit fields).

The "hdr" member of the union should contain any field which is global to all commands and which generic code (i.e. which isn't aware which command is in the cmd in its hand) can use. Off the top of my head that is just the cmd code itself.

You should also consider doing the collection sync in the caller as appropriate instead of pushing it down into its_send_single_command. IMHO its_send_single_command should do just that, not optionally do some other command too.

Ian.

#21 lore raw

Re: [RFC PATCH v3 05/18] xen/arm: ITS: Port ITS driver to xen

Quoted 69 lines · Vijay Kilari
Quoted 68 lines · Ian Campbell

On Mon, 2015-06-22 at 17:31 +0530, [email protected] wrote:
[...]

Quoted 11 lines
xen/arch/arm/gic-v3-its.c new file +11 -0
+/*
+ * ITS command descriptors - parameters to be encoded in a command
+ * block.
+ */
+struct its_cmd_desc {
+    union {
+        struct {
+            struct its_collection *col;
+            u32 event_id;
+            u32 dev_id;
+        } its_inv_cmd;

[...]

Quoted 14 lines
xen/arch/arm/gic-v3-its.c new file +14 -0
+static struct its_collection *its_build_inv_cmd(its_cmd_block *cmd,
+                                                struct its_cmd_desc *desc)
+{
+    memset(cmd, 0x0, sizeof(its_cmd_block));
+    cmd->inv.cmd = GITS_CMD_INV;
+    cmd->inv.devid = desc->its_inv_cmd.dev_id;
+    cmd->inv.event = desc->its_inv_cmd.event_id;
+
+#ifdef DEBUG_GIC_ITS
+    dump_cmd(cmd);
+#endif
+
+    return desc->its_inv_cmd.col;
+}

[...]

Quoted 11 lines
xen/arch/arm/gic-v3-its.c new file +11 -0
+void its_send_inv(struct its_device *dev, struct its_collection *col,
+                  u32 event_id)
+{
+    struct its_cmd_desc desc;
+
+    desc.its_inv_cmd.dev_id = dev->device_id;
+    desc.its_inv_cmd.event_id = event_id;
+    desc.its_inv_cmd.col = col;
+
+    its_send_single_command(dev->its, its_build_inv_cmd, &desc);
+}

[...]

xen/include/asm-arm/gic-its.h line 109

Quoted 9 lines
xen/include/asm-arm/gic-its.h new file +9 -0
+typedef struct __packed {
+    u64 cmd:8;
+    u64 res1:24;
+    u64 devid:32;
+    u64 event:32;
+    u64 res2:32;
+    u64 res3:64;
+    u64 res4:64;
+}inv_cmd_t;

(I've trimmed this to just the INV command, but it's the same for all of them)

I suppose this is a mix of the way the Linux code was structured and my request to use a struct/union to encode these things, but I'm afraid this is not how I intended to suggest things be done.

What I expected was something analogous to the hsr or lpae_t types, e.g. a single:

union its_cmd {
    uint64_t bits[N];

    struct {
        uint8_t cmd;
        uint8_t pad[...];
    } hdr;
Quoted 9 lines
Quoted 8 lines
    struct {
        uint8_t cmd;
        uint8_t res1[3];
        uint32_t devid;
        uint32_t event;
        uint64_t res2[2];
    } inv;
};

Commands like MAPD has 40 bit ITT, 5 bit Size and 1 bit valid bit. So we have
to still manage with bit fields to manage with ease. So we have to use mix of bit fields
and normal types (uint8_t & uint32_t etc.,)

#22 lore raw

Re: [RFC PATCH v3 05/18] xen/arm: ITS: Port ITS driver to xen

Quoted 80 lines · Vijay Kilari
Quoted 69 lines · Vijay Kilari
Quoted 68 lines · Ian Campbell

On Mon, 2015-06-22 at 17:31 +0530, [email protected] wrote:
[...]

Quoted 11 lines
xen/arch/arm/gic-v3-its.c new file +11 -0
+/*
+ * ITS command descriptors - parameters to be encoded in a command
+ * block.
+ */
+struct its_cmd_desc {
+    union {
+        struct {
+            struct its_collection *col;
+            u32 event_id;
+            u32 dev_id;
+        } its_inv_cmd;

[...]

Quoted 14 lines
xen/arch/arm/gic-v3-its.c new file +14 -0
+static struct its_collection *its_build_inv_cmd(its_cmd_block *cmd,
+                                                struct its_cmd_desc *desc)
+{
+    memset(cmd, 0x0, sizeof(its_cmd_block));
+    cmd->inv.cmd = GITS_CMD_INV;
+    cmd->inv.devid = desc->its_inv_cmd.dev_id;
+    cmd->inv.event = desc->its_inv_cmd.event_id;
+
+#ifdef DEBUG_GIC_ITS
+    dump_cmd(cmd);
+#endif
+
+    return desc->its_inv_cmd.col;
+}

[...]

Quoted 11 lines
xen/arch/arm/gic-v3-its.c new file +11 -0
+void its_send_inv(struct its_device *dev, struct its_collection *col,
+                  u32 event_id)
+{
+    struct its_cmd_desc desc;
+
+    desc.its_inv_cmd.dev_id = dev->device_id;
+    desc.its_inv_cmd.event_id = event_id;
+    desc.its_inv_cmd.col = col;
+
+    its_send_single_command(dev->its, its_build_inv_cmd, &desc);
+}

[...]

xen/include/asm-arm/gic-its.h line 109

Quoted 9 lines
xen/include/asm-arm/gic-its.h new file +9 -0
+typedef struct __packed {
+    u64 cmd:8;
+    u64 res1:24;
+    u64 devid:32;
+    u64 event:32;
+    u64 res2:32;
+    u64 res3:64;
+    u64 res4:64;
+}inv_cmd_t;

(I've trimmed this to just the INV command, but it's the same for all of them)

I suppose this is a mix of the way the Linux code was structured and my request to use a struct/union to encode these things, but I'm afraid this is not how I intended to suggest things be done.

What I expected was something analogous to the hsr or lpae_t types, e.g. a single:

union its_cmd {
    uint64_t bits[N];

    struct {
        uint8_t cmd;
        uint8_t pad[...];
    } hdr;
Quoted 9 lines
Quoted 8 lines
    struct {
        uint8_t cmd;
        uint8_t res1[3];
        uint32_t devid;
        uint32_t event;
        uint64_t res2[2];
    } inv;
};
Quoted 4 lines

Commands like MAPD has 40 bit ITT, 5 bit Size and 1 bit valid bit. So we have
to still manage with bit fields to manage with ease. So we have to use mix of bit fields
and normal types (uint8_t & uint32_t etc.,)

That's fine. Just use normal types for fields are sized that way and bitfields for the rest. e.g. for mapd:

    struct {
        uint8_t cmd;
        uint8_t res1[3];
        uint32_t devid;
        uint64_t size:5;
        uint64_t res2:59;
...
    };

Looking at ITT_addr you might consider including the res0 bits in bits 0..7 of that double word in the itt_addr, allowing you to avoid a shift on use of that field (this assumes that the reason for those res0 bits is the alignment constraints on itt_addr).

Ian.

#23 lore raw

RFC PATCH v3 6/18 xen/arm: ITS: Add helper functions to manage its_devices

In reply to [email protected]

From: Vijaya Kumar K <[email protected]>

Helper functions to mange its devices using RB-tree are introduced in physical ITS driver.

This is global list of all the devices.

---
 xen/arch/arm/gic-v3-its.c     |   49 +++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h |    3 +++
 2 files changed, 52 insertions(+)
xen/arch/arm/gic-v3-its.c +49 -0
index b1a97c1..349d0bb 100644
@@ -92,6 +92,7 @@ struct its_node {
 static LIST_HEAD(its_nodes);
 static DEFINE_SPINLOCK(its_lock);
 static struct rdist_prop  *gic_rdists;
+static struct rb_root rb_its_dev;
 
 #define gic_data_rdist()            (per_cpu(rdist, smp_processor_id()))
 #define gic_data_rdist_rd_base()    (per_cpu(rdist, smp_processor_id()).rbase)
@@ -145,6 +146,53 @@ void dump_cmd(its_cmd_block *cmd)
 }
 #endif
 
+/* RB-tree helpers for its_device */
+struct its_device * find_its_device(struct rb_root *root, u32 devid)
+{
+    struct rb_node *node = root->rb_node;
+
+    while ( node )
+    {
+        struct its_device *dev;
+
+        dev = container_of(node, struct its_device, node);
+        if ( devid < dev->device_id )
+            node = node->rb_left;
+        else if ( devid > dev->device_id )
+            node = node->rb_right;
+        else
+            return dev;
+    }
+
+    return NULL;
+}
+
+int insert_its_device(struct rb_root *root, struct its_device *dev)
+{
+    struct rb_node **new, *parent;
+
+    new = &root->rb_node;
+    parent = NULL;
+    while ( *new )
+    {
+        struct its_device *this;
+
+        this  = container_of(*new, struct its_device, node);
+        parent = *new;
+        if ( dev->device_id < this->device_id )
+            new = &((*new)->rb_left);
+        else if ( dev->device_id > this->device_id )
+            new = &((*new)->rb_right);
+        else
+            return -EEXIST;
+    }
+
+    rb_link_node(&dev->node, parent, new);
+    rb_insert_color(&dev->node, root);
+
+    return 0;
+}
+
 #define ITS_CMD_QUEUE_SZ            SZ_64K
 #define ITS_CMD_QUEUE_NR_ENTRIES    (ITS_CMD_QUEUE_SZ / sizeof(its_cmd_block))
 
@@ -994,6 +1042,7 @@ static int its_probe(struct dt_device_node *node)
     list_add(&its->entry, &its_nodes);
     spin_unlock(&its_lock);
 
+    rb_its_dev = RB_ROOT;
     return 0;
 
 out_free_tables:
xen/include/asm-arm/gic-its.h +3 -0
index 4e42f7f..59a6490 100644
@@ -19,6 +19,7 @@
 #define __ASM_ARM_GIC_ITS_H__
 
 #include <asm/gic_v3_defs.h>
+#include <xen/rbtree.h>
 
 /*
  * Collection structure - just an ID, and a redistributor address to
@@ -195,6 +196,8 @@ struct its_device {
     u32                     nr_ites;
     /* Physical Device id */
     u32                     device_id;
+    /* RB-tree entry */
+    struct rb_node          node;
 };
 
 static inline uint8_t its_decode_cmd(its_cmd_block *cmd)
Signature · 1 line
1.7.9.5
#24 lore raw

Re: [RFC PATCH v3 06/18] xen/arm: ITS: Add helper functions to manage its_devices

Hi Vijay,

Quoted 3 lines · On 22/06/2015 13:01, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

Helper functions to mange its devices using RB-tree

s/mange/manage/

Quoted 23 lines

are introduced in physical ITS driver.

This is global list of all the devices.

---
  xen/arch/arm/gic-v3-its.c     |   49 +++++++++++++++++++++++++++++++++++++++++
  xen/include/asm-arm/gic-its.h |    3 +++
  2 files changed, 52 insertions(+)
xen/arch/arm/gic-v3-its.c +1 -0
index b1a97c1..349d0bb 100644
@@ -92,6 +92,7 @@ struct its_node {
  static LIST_HEAD(its_nodes);
  static DEFINE_SPINLOCK(its_lock);
  static struct rdist_prop  *gic_rdists;
+static struct rb_root rb_its_dev;
 
  #define gic_data_rdist()            (per_cpu(rdist, smp_processor_id()))
  #define gic_data_rdist_rd_base()    (per_cpu(rdist, smp_processor_id()).rbase)

xen/arch/arm/gic-v3-its.c void dump_cmd(its_cmd_block *cmd) · line 150

Quoted 5 lines
xen/arch/arm/gic-v3-its.c +2 -0
@@ -145,6 +146,53 @@ void dump_cmd(its_cmd_block *cmd)
  }
  #endif
 
+/* RB-tree helpers for its_device */
+struct its_device * find_its_device(struct rb_root *root, u32 devid)

coding style: struct its_device *find

I would rename to its_find_device to keep consistent with the function name within this file.

Also, for any exported function you have to declare the prototype in the header within the same patch.

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+{
+    struct rb_node *node = root->rb_node;
+
+    while ( node )
+    {
+        struct its_device *dev;
+
+        dev = container_of(node, struct its_device, node);
+        if ( devid < dev->device_id )
+            node = node->rb_left;

xen/arch/arm/gic-v3-its.c void dump_cmd(its_cmd_block *cmd) · line 170

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+        else if ( devid > dev->device_id )
+            node = node->rb_right;
+        else
+            return dev;
+    }
+
+    return NULL;
+}
+
+int insert_its_device(struct rb_root *root, struct its_device *dev)

Why do you need the root in parameter? You already it within the file.

Also, I would rename to its_add_device.

Regards,

Signature · 1 line
Julien Grall
#25 lore raw

RFC PATCH v3 7/18 xen/arm: ITS: implement hw_irq_controller for LPIs

In reply to [email protected]

From: Vijaya Kumar K <[email protected]>

Implements hw_irq_controller api's required
to handle LPI's

---
 xen/arch/arm/gic-v3-its.c     |   39 +++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c         |   26 +++++++++++++++++++-------
 xen/arch/arm/irq.c            |   16 ++++++++++++++++
 xen/include/asm-arm/gic-its.h |   10 ++++++++++
 xen/include/asm-arm/gic.h     |    4 ++++
 xen/include/asm-arm/irq.h     |    4 +++-
 6 files changed, 91 insertions(+), 8 deletions(-)
xen/arch/arm/gic-v3-its.c +39 -0
index 349d0bb..535fc53 100644
@@ -508,6 +508,45 @@ static void its_send_invall(struct its_node *its, struct its_collection *col)
     its_send_single_command(its, its_build_invall_cmd, &desc);
 }
 
+void lpi_set_config(struct irq_desc *desc, int enable)
+{
+    struct its_collection *col;
+    struct its_device *its_dev = get_irq_device(desc);
+    u8 *cfg;
+    u32 virq = irq_to_virq(desc);
+
+    ASSERT(virq < its_dev->nr_lpis);
+
+    cfg = gic_rdists->prop_page + desc->irq - NR_GIC_LPI;
+    if ( enable )
+        *cfg |= LPI_PROP_ENABLED;
+    else
+        *cfg &= ~LPI_PROP_ENABLED;
+
+    /*
+     * Make the above write visible to the redistributors.
+     * And yes, we're flushing exactly: One. Single. Byte.
+     * Humpf...
+     */
+    if ( gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING )
+        clean_and_invalidate_dcache_va_range(cfg, sizeof(*cfg));
+    else
+        dsb(ishst);
+
+    /* Get collection id for this event id */
+    col = &its_dev->its->collections[virq % num_online_cpus()];
+    its_send_inv(its_dev, col, virq);
+}
+
+void its_set_affinity(struct irq_desc *desc, int cpu)
+{
+    struct its_device *its_dev = get_irq_device(desc);
+    struct its_collection *target_col;
+
+    /* Physical collection id */
+    target_col = &its_dev->its->collections[cpu];
+    its_send_movi(its_dev, target_col, irq_to_virq(desc));
+}
 /*
  * How we allocate LPIs:
  *
xen/arch/arm/gic-v3.c +19 -7
index b5c59f6..556b291 100644
@@ -40,6 +40,7 @@
 #include <asm/device.h>
 #include <asm/gic.h>
 #include <asm/gic_v3_defs.h>
+#include <asm/gic-its.h>
 #include <asm/cpufeature.h>
 
 struct rdist_region {
@@ -427,12 +428,18 @@ static void gicv3_poke_irq(struct irq_desc *irqd, u32 offset)
 
 static void gicv3_unmask_irq(struct irq_desc *irqd)
 {
-    gicv3_poke_irq(irqd, GICD_ISENABLER);
+    if ( is_lpi(irqd->irq) )
+        lpi_set_config(irqd, 1);
+    else
+        gicv3_poke_irq(irqd, GICD_ISENABLER);
 }
 
 static void gicv3_mask_irq(struct irq_desc *irqd)
 {
-    gicv3_poke_irq(irqd, GICD_ICENABLER);
+    if ( is_lpi(irqd->irq) )
+        lpi_set_config(irqd, 0);
+    else
+        gicv3_poke_irq(irqd, GICD_ICENABLER);
 }
 
 static void gicv3_eoi_irq(struct irq_desc *irqd)
@@ -1095,13 +1102,18 @@ static void gicv3_irq_set_affinity(struct irq_desc *desc, const cpumask_t *mask)
     spin_lock(&gicv3.lock);
 
     cpu = gicv3_get_cpu_from_mask(mask);
-    affinity = gicv3_mpidr_to_affinity(cpu);
-    /* Make sure we don't broadcast the interrupt */
-    affinity &= ~GICD_IROUTER_SPI_MODE_ANY;
 
-    if ( desc->irq >= NR_GIC_LOCAL_IRQS )
-        writeq_relaxed(affinity, (GICD + GICD_IROUTER + desc->irq * 8));
+    if ( is_lpi(desc->irq) )
+        its_set_affinity(desc, cpu);
+    else
+    {
+        affinity = gicv3_mpidr_to_affinity(cpu);
+        /* Make sure we don't broadcast the interrupt */
+        affinity &= ~GICD_IROUTER_SPI_MODE_ANY;
 
+        if ( desc->irq >= NR_GIC_LOCAL_IRQS )
+            writeq_relaxed(affinity, (GICD + GICD_IROUTER + desc->irq * 8));
+    }
     spin_unlock(&gicv3.lock);
 }
 
xen/arch/arm/irq.c +16 -0
index 2dd43ee..9dbdf7d 100644
@@ -36,6 +36,7 @@ struct irq_guest
 {
     struct domain *d;
     unsigned int virq;
+    struct its_device *dev;
 };
 
 static void ack_none(struct irq_desc *irq)
@@ -143,6 +144,21 @@ static inline struct domain *irq_get_domain(struct irq_desc *desc)
     return irq_get_guest_info(desc)->d;
 }
 
+unsigned int irq_to_virq(struct irq_desc *desc)
+{
+    return irq_get_guest_info(desc)->virq;
+}
+
+struct its_device *get_irq_device(struct irq_desc *desc)
+{
+    return irq_get_guest_info(desc)->dev;
+}
+
+void set_irq_device(struct irq_desc *desc, struct its_device *dev)
+{
+    irq_get_guest_info(desc)->dev = dev;
+}
+
 void irq_set_affinity(struct irq_desc *desc, const cpumask_t *cpu_mask)
 {
     if ( desc != NULL )
xen/include/asm-arm/gic-its.h +10 -0
index 59a6490..a47cf26 100644
@@ -205,6 +205,16 @@ static inline uint8_t its_decode_cmd(its_cmd_block *cmd)
     return cmd->raw_cmd[0] & 0xff;
 }
 
+static inline uint32_t its_decode_devid(struct domain *d, its_cmd_block *cmd)
+{
+    /* TODO: Use pci helper function to get physical id */
+    return (cmd->raw_cmd[0] >> 32);  
+}
+
+void its_set_affinity(struct irq_desc *desc, int cpu);
+void lpi_set_config(struct irq_desc *desc, int enable);
+uint32_t its_get_nr_events(void);
+
 #endif /* __ASM_ARM_GIC_ITS_H__ */
 
 /*
xen/include/asm-arm/gic.h +4 -0
index e9d5f36..0209cc5 100644
@@ -20,6 +20,9 @@
 
 #define NR_GIC_LOCAL_IRQS  NR_LOCAL_IRQS
 #define NR_GIC_SGI         16
+#define NR_GIC_LPI         8192
+#define MAX_LPI            (8192 + 4096)
+#define MAX_NR_LPIS        4096
 #define MAX_RDIST_COUNT    4
 
 #define GICD_CTLR       (0x000)
@@ -163,6 +166,7 @@
 #define DT_MATCH_GIC_V3 DT_MATCH_COMPATIBLE("arm,gic-v3")
 #define DT_MATCH_GIC_ITS DT_MATCH_COMPATIBLE("arm,gic-v3-its")
 
+#define is_lpi(lpi) (lpi >= NR_GIC_LPI && lpi < MAX_LPI)
 /*
  * GICv3 registers that needs to be saved/restored
  */
xen/include/asm-arm/irq.h +3 -1
index 34b492b..3b29182 100644
@@ -50,7 +50,9 @@ void arch_move_irqs(struct vcpu *v);
 
 /* Set IRQ type for an SPI */
 int irq_set_spi_type(unsigned int spi, unsigned int type);
-
+unsigned int irq_to_virq(struct irq_desc *desc);
+struct its_device *get_irq_device(struct irq_desc *desc);
+void set_irq_device(struct irq_desc *desc, struct its_device *dev);
 int platform_get_irq(const struct dt_device_node *device, int index);
 
 void irq_set_affinity(struct irq_desc *desc, const cpumask_t *cpu_mask);
Signature · 1 line
1.7.9.5
#26 lore raw

Re: [RFC PATCH v3 07/18] xen/arm: ITS: implement hw_irq_controller for LPIs

Hi Vijay,

Quoted 4 lines · On 22/06/15 13:01, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

Implements hw_irq_controller api's required
to handle LPI's

This patch doesn't hw_irq_controller for LPI but just hack around the current GICv3 host hw_irq_controller.

As said on the previous version, the goal of hw_irq_controller is too keep things simple (i.e few conditional code). Please introduce a separate hw_irq_controller for LPIs.

Quoted 16 lines
---
 xen/arch/arm/gic-v3-its.c     |   39 +++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c         |   26 +++++++++++++++++++-------
 xen/arch/arm/irq.c            |   16 ++++++++++++++++
 xen/include/asm-arm/gic-its.h |   10 ++++++++++
 xen/include/asm-arm/gic.h     |    4 ++++
 xen/include/asm-arm/irq.h     |    4 +++-
 6 files changed, 91 insertions(+), 8 deletions(-)
xen/arch/arm/gic-v3-its.c +0 -0
index 349d0bb..535fc53 100644

xen/arch/arm/gic-v3-its.c static void its_send_invall(struct its_node *its, struct its_collection *col) · line 511

Quoted 4 lines
xen/arch/arm/gic-v3-its.c +1 -0
@@ -508,6 +508,45 @@ static void its_send_invall(struct its_node *its, struct its_collection *col)
     its_send_single_command(its, its_build_invall_cmd, &desc);
 }
 
+void lpi_set_config(struct irq_desc *desc, int enable)

Any function exported should have their prototype defined within the same patch...

Quoted 16 lines
xen/arch/arm/gic-v3-its.c +16 -0
+{
+    struct its_collection *col;
+    struct its_device *its_dev = get_irq_device(desc);
+    u8 *cfg;
+    u32 virq = irq_to_virq(desc);
+
+    ASSERT(virq < its_dev->nr_lpis);
+
+    cfg = gic_rdists->prop_page + desc->irq - NR_GIC_LPI;
+    if ( enable )
+        *cfg |= LPI_PROP_ENABLED;
+    else
+        *cfg &= ~LPI_PROP_ENABLED;
+
+    /*
+     * Make the above write visible to the redistributors.

xen/arch/arm/gic-v3-its.c static void its_send_invall(struct its_node *its, struct its_collection *col) · line 537

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+     * And yes, we're flushing exactly: One. Single. Byte.
+     * Humpf...
+     */
+    if ( gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING )
+        clean_and_invalidate_dcache_va_range(cfg, sizeof(*cfg));
+    else
+        dsb(ishst);
+
+    /* Get collection id for this event id */
+    col = &its_dev->its->collections[virq % num_online_cpus()];

This is fragile, you are assuming that num_online_cpus() will never change. Why don't you store the collection in every irq_desc?

xen/arch/arm/gic-v3-its.c static void its_send_invall(struct its_node *its, struct its_collection *col) · line 548

Quoted 11 lines
xen/arch/arm/gic-v3-its.c +11 -0
+    its_send_inv(its_dev, col, virq);
+}
+
+void its_set_affinity(struct irq_desc *desc, int cpu)
+{
+    struct its_device *its_dev = get_irq_device(desc);
+    struct its_collection *target_col;
+
+    /* Physical collection id */
+    target_col = &its_dev->its->collections[cpu];
+    its_send_movi(its_dev, target_col, irq_to_virq(desc));

The field "virq" in the structure irq_guest refers to the guest virtual IRQ and not the event ID. As Ian suggested in the proposal [1], please use an union to make this code clears.

Furthermore, when you set the LPI configuration (see lpi_set_config) you are using a round robin to get the collection. This won't work anymore if Xen decides to change the affinity... So you may want to drop affinity support for now.

xen/arch/arm/gic-v3-its.c static void its_send_invall(struct its_node *its, struct its_collection *col) · line 549

Quoted 1 line
xen/arch/arm/gic-v3-its.c +1 -0
+}

Missing newline.

[..]

xen/arch/arm/irq.c struct irq_guest · line 39

Quoted 9 lines
xen/arch/arm/irq.c +1 -0
index 2dd43ee..9dbdf7d 100644
@@ -36,6 +36,7 @@ struct irq_guest
 {
     struct domain *d;
     unsigned int virq;
+    struct its_device *dev;

I know that this was suggested in the proposal [1]. But the goal of irq_guest is to store anything specific to the guest. The event ID and the its_device assigned are known when the device is added to Xen and hence can be set in irq_desc (with a small memory impact, but we have plenty of memory on ARM64).

This would avoid you to set dev and virq every time the device is passthrough to a guest.

Quoted 11 lines
xen/arch/arm/irq.c +4 -0
 };
 
 static void ack_none(struct irq_desc *irq)
@@ -143,6 +144,21 @@ static inline struct domain *irq_get_domain(struct irq_desc *desc)
     return irq_get_guest_info(desc)->d;
 }
 
+unsigned int irq_to_virq(struct irq_desc *desc)
+{
+    return irq_get_guest_info(desc)->virq;
+}

xen/arch/arm/irq.c static inline struct domain *irq_get_domain(struct irq_desc *desc) · line 160

Quoted 10 lines
xen/arch/arm/irq.c +10 -0
+
+struct its_device *get_irq_device(struct irq_desc *desc)
+{
+    return irq_get_guest_info(desc)->dev;
+}
+
+void set_irq_device(struct irq_desc *desc, struct its_device *dev)
+{
+    irq_get_guest_info(desc)->dev = dev;
+}

The goal of route_irq_guest is to setup correctly irq_guest. If the current version doesn't fit your usage please update it or add a new helper and no workaround the code.

Quoted 9 lines
xen/arch/arm/irq.c +1 -0
+
 void irq_set_affinity(struct irq_desc *desc, const cpumask_t *cpu_mask)
 {
     if ( desc != NULL )
xen/include/asm-arm/gic-its.h +0 -0
index 59a6490..a47cf26 100644

xen/include/asm-arm/gic-its.h static inline uint8_t its_decode_cmd(its_cmd_block *cmd) · line 212

Quoted 8 lines
xen/include/asm-arm/gic-its.h +5 -0
@@ -205,6 +205,16 @@ static inline uint8_t its_decode_cmd(its_cmd_block *cmd)
     return cmd->raw_cmd[0] & 0xff;
 }
 
+static inline uint32_t its_decode_devid(struct domain *d, its_cmd_block *cmd)
+{
+    /* TODO: Use pci helper function to get physical id */
+    return (cmd->raw_cmd[0] >> 32);  
+}

This doesn't belong to this patch. And without more comment it makes little sense why you are using cmd.

Quoted 13 lines
xen/include/asm-arm/gic-its.h +5 -0
+
+void its_set_affinity(struct irq_desc *desc, int cpu);
+void lpi_set_config(struct irq_desc *desc, int enable);
+uint32_t its_get_nr_events(void);
+
 #endif /* __ASM_ARM_GIC_ITS_H__ */
 
 /*
xen/include/asm-arm/gic.h +0 -0
index e9d5f36..0209cc5 100644

xen/include/asm-arm/gic.h line 25

Quoted 6 lines
xen/include/asm-arm/gic.h +3 -0
@@ -20,6 +20,9 @@
 
 #define NR_GIC_LOCAL_IRQS  NR_LOCAL_IRQS
 #define NR_GIC_SGI         16
+#define NR_GIC_LPI         8192
+#define MAX_LPI            (8192 + 4096)
+#define MAX_NR_LPIS        4096

For me NR_GIC_LPI, MAX_LPI and MAX_NR_LPIS means the exactly the same things but you are using 3 different value.

Please name the correctly:
- NR_GIC_LPI => FIRST_GIC_LPI
- MAX_NR_LPIS => NR_GIC_LPI
- Drop MAX_LPI

Although, why do you hardcode the number of LPIs? This should be retrieved from the GIC hardware configuration.

xen/include/asm-arm/gic.h line 169

Quoted 8 lines
xen/include/asm-arm/gic.h +1 -0
 #define MAX_RDIST_COUNT    4
 
 #define GICD_CTLR       (0x000)
@@ -163,6 +166,7 @@
 #define DT_MATCH_GIC_V3 DT_MATCH_COMPATIBLE("arm,gic-v3")
 #define DT_MATCH_GIC_ITS DT_MATCH_COMPATIBLE("arm,gic-v3-its")
 
+#define is_lpi(lpi) (lpi >= NR_GIC_LPI && lpi < MAX_LPI)

Missing newline.

Regards,

[1]
http://xenbits.xensource.com/people/ianc/vits/draftF.html#virtual-lpi-interrupt-injection

Signature · 1 line
Julien Grall
#27 lore raw

Re: [RFC PATCH v3 07/18] xen/arm: ITS: implement hw_irq_controller for LPIs

Quoted 8 lines · Julien Grall

Hi Vijay,

Quoted 4 lines · On 22/06/15 13:01, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

Implements hw_irq_controller api's required
to handle LPI's

Quoted 6 lines

This patch doesn't hw_irq_controller for LPI but just hack around the current GICv3 host hw_irq_controller.

As said on the previous version, the goal of hw_irq_controller is too keep things simple (i.e few conditional code). Please introduce a separate hw_irq_controller for LPIs.

If new hw_irq_controller is introduced for LPIs, then this has to be exported using some lpi structure which holds pointer to hw_irq_controller for guest & host type similar to gic_hw_ops

Quoted 21 lines
Quoted 21 lines
xen/arch/arm/gic-v3-its.c +21 -0
+{
+    struct its_collection *col;
+    struct its_device *its_dev = get_irq_device(desc);
+    u8 *cfg;
+    u32 virq = irq_to_virq(desc);
+
+    ASSERT(virq < its_dev->nr_lpis);
+
+    cfg = gic_rdists->prop_page + desc->irq - NR_GIC_LPI;
+    if ( enable )
+        *cfg |= LPI_PROP_ENABLED;
+    else
+        *cfg &= ~LPI_PROP_ENABLED;
+
+    /*
+     * Make the above write visible to the redistributors.
+     * And yes, we're flushing exactly: One. Single. Byte.
+     * Humpf...
+     */
+    if ( gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING )
+        clean_and_invalidate_dcache_va_range(cfg, sizeof(*cfg));
Quoted 8 lines

xen/arch/arm/gic-v3-its.c static void its_send_invall(struct its_node *its, struct its_collection *col) · line 537

Quoted 5 lines
xen/arch/arm/gic-v3-its.c +5 -0
+    else
+        dsb(ishst);
+
+    /* Get collection id for this event id */
+    col = &its_dev->its->collections[virq % num_online_cpus()];

This is fragile, you are assuming that num_online_cpus() will never change. Why don't you store the collection in every irq_desc?

This will add additional 8 bytes overhead for each irq_desc.

Also is there a macro to get number of actual number processors in system.? AUI, nr_cpu_ids always returns 128

Regards
Vijay

#28 lore raw

Re: [RFC PATCH v3 07/18] xen/arm: ITS: implement hw_irq_controller for LPIs

Hi Vijay,

Quoted 12 lines · Vijay Kilari
Quoted 11 lines · Julien Grall

Hi Vijay,

Quoted 4 lines · On 22/06/15 13:01, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

Implements hw_irq_controller api's required
to handle LPI's

This patch doesn't hw_irq_controller for LPI but just hack around the current GICv3 host hw_irq_controller.

Quoted 7 lines
Quoted 3 lines

As said on the previous version, the goal of hw_irq_controller is too keep things simple (i.e few conditional code). Please introduce a separate hw_irq_controller for LPIs.

If new hw_irq_controller is introduced for LPIs, then this has to be exported using some lpi structure which holds pointer to hw_irq_controller for guest & host type similar to gic_hw_ops

The interface is not set in stone, you are free to change what you want as long as we keep something clean and comprehensible. It's the same for the functions (I have in mind route_irq_to_guest).

In this case, I would prefer to see 2 callbacks (one for the host the other for the guest) which return the correct IRQ controller for a specific IRQ. I have in mind something like:

   get_guest_hw_irq_controller(unsigned int irq)
   {
       if ( !is_lpi )
         return &gicv3_guest_irq_controller
       else
         return &gicv3_guest_lpi_controller
   }

Same for the host irq controller. So the selection of the IRQ controller would be hidden from gic.c and keep the code a generic as possible.

Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
xen/arch/arm/gic-v3-its.c +12 -0
+    /*
+     * Make the above write visible to the redistributors.
+     * And yes, we're flushing exactly: One. Single. Byte.
+     * Humpf...
+     */
+    if ( gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING )
+        clean_and_invalidate_dcache_va_range(cfg, sizeof(*cfg));
+    else
+        dsb(ishst);
+
+    /* Get collection id for this event id */
+    col = &its_dev->its->collections[virq % num_online_cpus()];
Quoted 7 lines
Quoted 2 lines

This is fragile, you are assuming that num_online_cpus() will never change. Why don't you store the collection in every irq_desc?

This will add additional 8 bytes overhead for each irq_desc.

Also is there a macro to get number of actual number processors in system.? AUI, nr_cpu_ids always returns 128

AFAIU, nr_cpu_ids should reflect the number of CPU of the platform. x86 correctly set it when parsing the ACPI. So I think this is a bug in the ARM code.

In fact, I wasn't able to find a place in the ARM code where this value is changed.

Regards,

Signature · 1 line
Julien Grall
#29 lore raw

Re: [RFC PATCH v3 07/18] xen/arm: ITS: implement hw_irq_controller for LPIs

Quoted 19 lines · Julien Grall

Hi Vijay,

Quoted 16 lines · Vijay Kilari
Quoted 14 lines · Julien Grall

Hi Vijay,

Quoted 4 lines · On 22/06/15 13:01, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

Implements hw_irq_controller api's required
to handle LPI's

This patch doesn't hw_irq_controller for LPI but just hack around the current GICv3 host hw_irq_controller.

As said on the previous version, the goal of hw_irq_controller is too keep things simple (i.e few conditional code). Please introduce a separate hw_irq_controller for LPIs.

Quoted 7 lines
Quoted 3 lines

If new hw_irq_controller is introduced for LPIs, then this has to be exported using some lpi structure which holds pointer to hw_irq_controller for guest & host type similar to gic_hw_ops

The interface is not set in stone, you are free to change what you want as long as we keep something clean and comprehensible. It's the same for the functions (I have in mind route_irq_to_guest).

Ack.

Quoted 12 lines

In this case, I would prefer to see 2 callbacks (one for the host the other for the guest) which return the correct IRQ controller for a specific IRQ. I have in mind something like:

    get_guest_hw_irq_controller(unsigned int irq)
    {
        if ( !is_lpi )
          return &gicv3_guest_irq_controller
        else
          return &gicv3_guest_lpi_controller
    }
Quoted 2 lines

Same for the host irq controller. So the selection of the IRQ controller would be hidden from gic.c and keep the code a generic as possible.

Yes, this is how I would expect it too.

Alternatively I notice that the pattern today is:

        desc->handler = gic_hw_ops->gic_(host|guest)_irq_type;
     [set_bit(_IRQ_GUEST, &desc->status) or not]
     gic_set_irq_properties(desc,[...]);

So an alternative might be for the set_irq_properties hook in the ops to also setup the handler (based on desc->status&_IRQ_GUEST and desc->irq), perhaps renaming it to something less "property" based. Both callers are git_route_irq_to_... so perhaps gic_route_irq?

Ian.

#30 lore raw

Re: [RFC PATCH v3 07/18] xen/arm: ITS: implement hw_irq_controller for LPIs

Quoted 18 lines · Ian Campbell
Quoted 12 lines

In this case, I would prefer to see 2 callbacks (one for the host the other for the guest) which return the correct IRQ controller for a specific IRQ. I have in mind something like:

    get_guest_hw_irq_controller(unsigned int irq)
    {
        if ( !is_lpi )
          return &gicv3_guest_irq_controller
        else
          return &gicv3_guest_lpi_controller
    }
Quoted 2 lines

Same for the host irq controller. So the selection of the IRQ controller would be hidden from gic.c and keep the code a generic as possible.

Yes, this is how I would expect it too.

Alternatively I notice that the pattern today is:

Quoted 8 lines
        desc->handler = gic_hw_ops->gic_(host|guest)_irq_type;
     [set_bit(_IRQ_GUEST, &desc->status) or not]
     gic_set_irq_properties(desc,[...]);

So an alternative might be for the set_irq_properties hook in the ops to also setup the handler (based on desc->status&_IRQ_GUEST and desc->irq), perhaps renaming it to something less "property" based. Both callers are git_route_irq_to_... so perhaps gic_route_irq?

Sounds good for me.

Regards,

Signature · 1 line
Julien Grall
#31 lore raw

Re: [RFC PATCH v3 07/18] xen/arm: ITS: implement hw_irq_controller for LPIs

In reply to Julien Grall
Quoted 75 lines · Julien Grall

Hi Vijay,

Quoted 22 lines · Vijay Kilari
Quoted 11 lines · Julien Grall

Hi Vijay,

Quoted 5 lines · On 22/06/15 13:01, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

Implements hw_irq_controller api's required
to handle LPI's

Quoted 6 lines

This patch doesn't hw_irq_controller for LPI but just hack around the current GICv3 host hw_irq_controller.

As said on the previous version, the goal of hw_irq_controller is too keep things simple (i.e few conditional code). Please introduce a separate hw_irq_controller for LPIs.

Quoted 4 lines

If new hw_irq_controller is introduced for LPIs, then this has to be exported using some lpi structure which holds pointer to hw_irq_controller
for guest & host type similar to gic_hw_ops

The interface is not set in stone, you are free to change what you want as long as we keep something clean and comprehensible. It's the same for the functions (I have in mind route_irq_to_guest).

In this case, I would prefer to see 2 callbacks (one for the host the other for the guest) which return the correct IRQ controller for a specific IRQ. I have in mind something like:

   get_guest_hw_irq_controller(unsigned int irq)
   {
       if ( !is_lpi )
         return &gicv3_guest_irq_controller
       else
         return &gicv3_guest_lpi_controller
   }

Same for the host irq controller. So the selection of the IRQ controller would be hidden from gic.c and keep the code a generic as possible.

Quoted 18 lines
Quoted 14 lines

xen/arch/arm/gic-v3-its.c static void its_send_invall(struct its_node *its, struct its_collection *col) · line 537

Quoted 12 lines
xen/arch/arm/gic-v3-its.c +12 -0
+    /*
+     * Make the above write visible to the redistributors.
+     * And yes, we're flushing exactly: One. Single. Byte.
+     * Humpf...
+     */
+    if ( gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING )
+        clean_and_invalidate_dcache_va_range(cfg, sizeof(*cfg));
+    else
+        dsb(ishst);
+
+    /* Get collection id for this event id */
+    col = &its_dev->its->collections[virq % num_online_cpus()];
Quoted 2 lines

This is fragile, you are assuming that num_online_cpus() will never change. Why don't you store the collection in every irq_desc?

Quoted 5 lines

This will add additional 8 bytes overhead for each irq_desc.

Also is there a macro to get number of actual number processors in system.?
AUI, nr_cpu_ids always returns 128

Quoted 6 lines

AFAIU, nr_cpu_ids should reflect the number of CPU of the platform. x86 correctly set it when parsing the ACPI. So I think this is a bug in the ARM code.

In fact, I wasn't able to find a place in the ARM code where this value is changed.

nr_cpu_ids is not changed in case of ARM. I think this value has to be updated in start_xen with cpus value

void __init start_xen(unsigned long boot_phys_offset,
unsigned long fdt_paddr, unsigned long cpuid)

....
cpus = smp_get_max_cpus();
..

}

#32 lore raw

Re: [RFC PATCH v3 07/18] xen/arm: ITS: implement hw_irq_controller for LPIs

In reply to Julien Grall

Hi Julien,

Quoted 7 lines
Quoted 7 lines
xen/arch/arm/gic-v3-its.c +7 -0
+    its_send_inv(its_dev, col, virq);
+}
+
+void its_set_affinity(struct irq_desc *desc, int cpu)
+{
+    struct its_device *its_dev = get_irq_device(desc);
+    struct its_collection *target_col;
Quoted 8 lines

xen/arch/arm/gic-v3-its.c static void its_send_invall(struct its_node *its, struct its_collection *col) · line 548

Quoted 4 lines
xen/arch/arm/gic-v3-its.c +4 -0
+
+    /* Physical collection id */
+    target_col = &its_dev->its->collections[cpu];
+    its_send_movi(its_dev, target_col, irq_to_virq(desc));

The field "virq" in the structure irq_guest refers to the guest virtual IRQ and not the event ID. As Ian suggested in the proposal [1], please use an union to make this code clears.

Apart from adding union, do you recommend to add macros irq_to_vid()
and irq_to_virq() and use appropriately?

Quoted 12 lines

Furthermore, when you set the LPI configuration (see lpi_set_config) you are using a round robin to get the collection. This won't work anymore if Xen decides to change the affinity... So you may want to drop affinity support for now.

Quoted 1 line

+}

Missing newline.

[..]

Quoted 15 lines

xen/arch/arm/irq.c struct irq_guest · line 39

Quoted 9 lines
xen/arch/arm/irq.c +1 -0
index 2dd43ee..9dbdf7d 100644
@@ -36,6 +36,7 @@ struct irq_guest
 {
     struct domain *d;
     unsigned int virq;
+    struct its_device *dev;

I know that this was suggested in the proposal [1]. But the goal of irq_guest is to store anything specific to the guest. The event ID and the its_device assigned are known when the device is added to Xen and hence can be set in irq_desc (with a small memory impact, but we have plenty of memory on ARM64).

Do you mean adding its_device to irq_desc instead of irq_guest? If so, irq_desc is common for both x86 & ARM.

Regards
Vijay

#33 lore raw

Re: [RFC PATCH v3 07/18] xen/arm: ITS: implement hw_irq_controller for LPIs

Quoted 1 line · Vijay Kilari

Hi Julien,

Hi Vijay,

Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
xen/arch/arm/gic-v3-its.c +7 -0
+    its_send_inv(its_dev, col, virq);
+}
+
+void its_set_affinity(struct irq_desc *desc, int cpu)
+{
+    struct its_device *its_dev = get_irq_device(desc);
+    struct its_collection *target_col;
Quoted 8 lines

xen/arch/arm/gic-v3-its.c static void its_send_invall(struct its_node *its, struct its_collection *col) · line 548

Quoted 4 lines
xen/arch/arm/gic-v3-its.c +4 -0
+
+    /* Physical collection id */
+    target_col = &its_dev->its->collections[cpu];
+    its_send_movi(its_dev, target_col, irq_to_virq(desc));

The field "virq" in the structure irq_guest refers to the guest virtual IRQ and not the event ID. As Ian suggested in the proposal [1], please use an union to make this code clears.

Quoted 2 lines

Apart from adding union, do you recommend to add macros irq_to_vid()
and irq_to_virq() and use appropriately?

If it makes to code clearer yes. Although it may not be necessary if we move the value into irq_desc (see below).

[..]

Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
xen/arch/arm/irq.c +0 -0
index 2dd43ee..9dbdf7d 100644
@@ -36,6 +36,7 @@ struct irq_guest
  {
      struct domain *d;
Quoted 8 lines

xen/arch/arm/irq.c struct irq_guest · line 39

Quoted 2 lines
xen/arch/arm/irq.c +1 -0
      unsigned int virq;
+    struct its_device *dev;

I know that this was suggested in the proposal [1]. But the goal of irq_guest is to store anything specific to the guest. The event ID and the its_device assigned are known when the device is added to Xen and hence can be set in irq_desc (with a small memory impact, but we have plenty of memory on ARM64).

Quoted 2 lines

Do you mean adding its_device to irq_desc instead of irq_guest? If so, irq_desc is common for both x86 & ARM.

There is an arch specific structure for irq_desc (see arch_irq_desc).

Regards,

Signature · 1 line
Julien Grall
#34 lore raw

Re: [RFC PATCH v3 07/18] xen/arm: ITS: implement hw_irq_controller for LPIs

In reply to Julien Grall

On Tue, 2015-06-23 at 15:32 +0100, Julien Grall wrote:
[...]

Quoted 21 lines
Quoted 21 lines
xen/arch/arm/gic-v3-its.c +21 -0
+{
+    struct its_collection *col;
+    struct its_device *its_dev = get_irq_device(desc);
+    u8 *cfg;
+    u32 virq = irq_to_virq(desc);
+
+    ASSERT(virq < its_dev->nr_lpis);
+
+    cfg = gic_rdists->prop_page + desc->irq - NR_GIC_LPI;
+    if ( enable )
+        *cfg |= LPI_PROP_ENABLED;
+    else
+        *cfg &= ~LPI_PROP_ENABLED;
+
+    /*
+     * Make the above write visible to the redistributors.
+     * And yes, we're flushing exactly: One. Single. Byte.
+     * Humpf...
+     */
+    if ( gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING )
+        clean_and_invalidate_dcache_va_range(cfg, sizeof(*cfg));
Quoted 8 lines

xen/arch/arm/gic-v3-its.c static void its_send_invall(struct its_node *its, struct its_collection *col) · line 537

Quoted 5 lines
xen/arch/arm/gic-v3-its.c +5 -0
+    else
+        dsb(ishst);
+
+    /* Get collection id for this event id */
+    col = &its_dev->its->collections[virq % num_online_cpus()];

This is fragile, you are assuming that num_online_cpus() will never change. Why don't you store the collection in every irq_desc?

The original Linux code upon which this is based doesn't seem to need to lookup the collection here, why is flushing needed for us but not Linux?

I'm also confused by the use of the variable name "virq" in a function called set_lpi_config which appears to be dealing with host level physical LPIs. It seems like this function would be broken for LPIs which were delivered to Xen and not to a guest, and that the irq_to_virq in here ought to be desc->irq, no?

BTW, the Linux original calls what this calls "virq" "id" instead, which is much less confusing.

#35 lore raw

Re: [RFC PATCH v3 07/18] xen/arm: ITS: implement hw_irq_controller for LPIs

Quoted 32 lines · Ian Campbell

On Tue, 2015-06-23 at 15:32 +0100, Julien Grall wrote:
[...]

Quoted 21 lines
Quoted 21 lines
xen/arch/arm/gic-v3-its.c +21 -0
+{
+    struct its_collection *col;
+    struct its_device *its_dev = get_irq_device(desc);
+    u8 *cfg;
+    u32 virq = irq_to_virq(desc);
+
+    ASSERT(virq < its_dev->nr_lpis);
+
+    cfg = gic_rdists->prop_page + desc->irq - NR_GIC_LPI;
+    if ( enable )
+        *cfg |= LPI_PROP_ENABLED;
+    else
+        *cfg &= ~LPI_PROP_ENABLED;
+
+    /*
+     * Make the above write visible to the redistributors.
+     * And yes, we're flushing exactly: One. Single. Byte.
+     * Humpf...
+     */
+    if ( gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING )
+        clean_and_invalidate_dcache_va_range(cfg, sizeof(*cfg));
Quoted 8 lines

xen/arch/arm/gic-v3-its.c static void its_send_invall(struct its_node *its, struct its_collection *col) · line 537

Quoted 5 lines
xen/arch/arm/gic-v3-its.c +5 -0
+    else
+        dsb(ishst);
+
+    /* Get collection id for this event id */
+    col = &its_dev->its->collections[virq % num_online_cpus()];

This is fragile, you are assuming that num_online_cpus() will never change. Why don't you store the collection in every irq_desc?

Quoted 2 lines

The original Linux code upon which this is based doesn't seem to need to lookup the collection here, why is flushing needed for us but not Linux?

We are writing to lpi property table. Even linux code flushes it.

Quoted 6 lines

I'm also confused by the use of the variable name "virq" in a function called set_lpi_config which appears to be dealing with host level physical LPIs. It seems like this function would be broken for LPIs which were delivered to Xen and not to a guest, and that the irq_to_virq in here ought to be desc->irq, no?

I am using desc->irq for updating lpi property table but using vid to send inv command

Quoted 3 lines

BTW, the Linux original calls what this calls "virq" "id" instead, which is much less confusing.

OK will rename virq to vid

#36 lore raw

Re: [RFC PATCH v3 07/18] xen/arm: ITS: implement hw_irq_controller for LPIs

Quoted 33 lines · Vijay Kilari
Quoted 32 lines · Ian Campbell

On Tue, 2015-06-23 at 15:32 +0100, Julien Grall wrote:
[...]

Quoted 29 lines
Quoted 16 lines
xen/arch/arm/gic-v3-its.c +16 -0
+{
+    struct its_collection *col;
+    struct its_device *its_dev = get_irq_device(desc);
+    u8 *cfg;
+    u32 virq = irq_to_virq(desc);
+
+    ASSERT(virq < its_dev->nr_lpis);
+
+    cfg = gic_rdists->prop_page + desc->irq - NR_GIC_LPI;
+    if ( enable )
+        *cfg |= LPI_PROP_ENABLED;
+    else
+        *cfg &= ~LPI_PROP_ENABLED;
+
+    /*
+     * Make the above write visible to the redistributors.

xen/arch/arm/gic-v3-its.c static void its_send_invall(struct its_node *its, struct its_collection *col) · line 537

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+     * And yes, we're flushing exactly: One. Single. Byte.
+     * Humpf...
+     */
+    if ( gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING )
+        clean_and_invalidate_dcache_va_range(cfg, sizeof(*cfg));
+    else
+        dsb(ishst);
+
+    /* Get collection id for this event id */
+    col = &its_dev->its->collections[virq % num_online_cpus()];

This is fragile, you are assuming that num_online_cpus() will never change. Why don't you store the collection in every irq_desc?

Quoted 4 lines
Quoted 2 lines

The original Linux code upon which this is based doesn't seem to need to lookup the collection here, why is flushing needed for us but not Linux?

We are writing to lpi property table. Even linux code flushes it.

Sorry I was referring to the collection look up and inv, not the cache flush, i.e. this bit:

+    /* Get collection id for this event id */
+    col = &its_dev->its->collections[virq % num_online_cpus()];
+    its_send_inv(its_dev, col, virq);

Linux doesn't seem to do that INV there.

Ian.

#37 lore raw

Re: [RFC PATCH v3 07/18] xen/arm: ITS: implement hw_irq_controller for LPIs

Quoted 46 lines · Ian Campbell
Quoted 33 lines · Vijay Kilari
Quoted 32 lines · Ian Campbell

On Tue, 2015-06-23 at 15:32 +0100, Julien Grall wrote:
[...]

Quoted 29 lines
Quoted 16 lines
xen/arch/arm/gic-v3-its.c +16 -0
+{
+    struct its_collection *col;
+    struct its_device *its_dev = get_irq_device(desc);
+    u8 *cfg;
+    u32 virq = irq_to_virq(desc);
+
+    ASSERT(virq < its_dev->nr_lpis);
+
+    cfg = gic_rdists->prop_page + desc->irq - NR_GIC_LPI;
+    if ( enable )
+        *cfg |= LPI_PROP_ENABLED;
+    else
+        *cfg &= ~LPI_PROP_ENABLED;
+
+    /*
+     * Make the above write visible to the redistributors.

xen/arch/arm/gic-v3-its.c static void its_send_invall(struct its_node *its, struct its_collection *col) · line 537

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+     * And yes, we're flushing exactly: One. Single. Byte.
+     * Humpf...
+     */
+    if ( gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING )
+        clean_and_invalidate_dcache_va_range(cfg, sizeof(*cfg));
+    else
+        dsb(ishst);
+
+    /* Get collection id for this event id */
+    col = &its_dev->its->collections[virq % num_online_cpus()];

This is fragile, you are assuming that num_online_cpus() will never change. Why don't you store the collection in every irq_desc?

Quoted 4 lines
Quoted 2 lines

The original Linux code upon which this is based doesn't seem to need to lookup the collection here, why is flushing needed for us but not Linux?

We are writing to lpi property table. Even linux code flushes it.

Sorry I was referring to the collection look up and inv, not the cache flush, i.e. this bit:

xen/arch/arm/gic-v3-its.c +3 -0
+    /* Get collection id for this event id */
+    col = &its_dev->its->collections[virq % num_online_cpus()];
+    its_send_inv(its_dev, col, virq);
Quoted 1 line

Linux doesn't seem to do that INV there.

Linux does INV.

https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/drivers/irqchip/irq-gic-v3-its.c?id=refs/tags/v4.1

line 555

#38 lore raw

Re: [RFC PATCH v3 07/18] xen/arm: ITS: implement hw_irq_controller for LPIs

Quoted 47 lines · Vijay Kilari
Quoted 46 lines · Ian Campbell
Quoted 33 lines · Vijay Kilari
Quoted 32 lines · Ian Campbell

On Tue, 2015-06-23 at 15:32 +0100, Julien Grall wrote:
[...]

Quoted 29 lines
Quoted 16 lines
xen/arch/arm/gic-v3-its.c +16 -0
+{
+    struct its_collection *col;
+    struct its_device *its_dev = get_irq_device(desc);
+    u8 *cfg;
+    u32 virq = irq_to_virq(desc);
+
+    ASSERT(virq < its_dev->nr_lpis);
+
+    cfg = gic_rdists->prop_page + desc->irq - NR_GIC_LPI;
+    if ( enable )
+        *cfg |= LPI_PROP_ENABLED;
+    else
+        *cfg &= ~LPI_PROP_ENABLED;
+
+    /*
+     * Make the above write visible to the redistributors.

xen/arch/arm/gic-v3-its.c static void its_send_invall(struct its_node *its, struct its_collection *col) · line 537

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+     * And yes, we're flushing exactly: One. Single. Byte.
+     * Humpf...
+     */
+    if ( gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING )
+        clean_and_invalidate_dcache_va_range(cfg, sizeof(*cfg));
+    else
+        dsb(ishst);
+
+    /* Get collection id for this event id */
+    col = &its_dev->its->collections[virq % num_online_cpus()];

This is fragile, you are assuming that num_online_cpus() will never change. Why don't you store the collection in every irq_desc?

Quoted 4 lines
Quoted 2 lines

The original Linux code upon which this is based doesn't seem to need to lookup the collection here, why is flushing needed for us but not Linux?

We are writing to lpi property table. Even linux code flushes it.

Sorry I was referring to the collection look up and inv, not the cache flush, i.e. this bit:

xen/arch/arm/gic-v3-its.c +3 -0
+    /* Get collection id for this event id */
+    col = &its_dev->its->collections[virq % num_online_cpus()];
+    its_send_inv(its_dev, col, virq);
Quoted 7 lines
Quoted 1 line

Linux doesn't seem to do that INV there.

Linux does INV.

https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/drivers/irqchip/irq-gic-v3-its.c?id=refs/tags/v4.1

line 555

So it does, not sure how I missed that when I first looked.

Linux's approach of saving collection in the its_dev seems preferable to looking it up like this here though.

Ian.

#39 lore raw

Re: [RFC PATCH v3 07/18] xen/arm: ITS: implement hw_irq_controller for LPIs

Quoted 52 lines · Ian Campbell
Quoted 51 lines · Vijay Kilari
Quoted 46 lines · Ian Campbell
Quoted 33 lines · Vijay Kilari
Quoted 32 lines · Ian Campbell

On Tue, 2015-06-23 at 15:32 +0100, Julien Grall wrote:
[...]

Quoted 29 lines
Quoted 16 lines
(quoted diff) +16 -0
+{
+    struct its_collection *col;
+    struct its_device *its_dev = get_irq_device(desc);
+    u8 *cfg;
+    u32 virq = irq_to_virq(desc);
+
+    ASSERT(virq < its_dev->nr_lpis);
+
+    cfg = gic_rdists->prop_page + desc->irq - NR_GIC_LPI;
+    if ( enable )
+        *cfg |= LPI_PROP_ENABLED;
+    else
+        *cfg &= ~LPI_PROP_ENABLED;
+
+    /*
+     * Make the above write visible to the redistributors.

Review

Quoted 10 lines
(quoted diff) +10 -0
+     * And yes, we're flushing exactly: One. Single. Byte.
+     * Humpf...
+     */
+    if ( gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING )
+        clean_and_invalidate_dcache_va_range(cfg, sizeof(*cfg));
+    else
+        dsb(ishst);
+
+    /* Get collection id for this event id */
+    col = &its_dev->its->collections[virq % num_online_cpus()];

This is fragile, you are assuming that num_online_cpus() will never change. Why don't you store the collection in every irq_desc?

Quoted 4 lines
Quoted 2 lines

The original Linux code upon which this is based doesn't seem to need to lookup the collection here, why is flushing needed for us but not Linux?

We are writing to lpi property table. Even linux code flushes it.

Sorry I was referring to the collection look up and inv, not the cache flush, i.e. this bit:

(quoted diff) +3 -0
+    /* Get collection id for this event id */
+    col = &its_dev->its->collections[virq % num_online_cpus()];
+    its_send_inv(its_dev, col, virq);
Quoted 1 line

Linux doesn't seem to do that INV there.

Linux does INV.

Quoted 8 lines
Quoted 3 lines

https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/drivers/irqchip/irq-gic-v3-its.c?id=refs/tags/v4.1

line 555

So it does, not sure how I missed that when I first looked.

Linux's approach of saving collection in the its_dev seems preferable to looking it up like this here though.

That would be a better idea. We have too many place in this series where the way to retrieve the collection for an interrupt.

Hence, the irq_desc should have a pointer to its_dev (as suggested in another patch).

struct device would even be better, but I can live with its_dev for now.

Regards,

Signature · 1 line
Julien Grall
#40 lore raw

RFC PATCH v3 8/18 xen/arm: vITS: Add virtual ITS driver

In reply to [email protected]

From: Vijaya Kumar K <[email protected]>

This patch introduces virtual ITS driver with following functionality
- Introduces helper functions to manage device table and ITT table in guest memory
- Helper function to handle virtual ITS devices assigned to domain

---
 xen/arch/arm/Makefile         |    1 +
 xen/arch/arm/vgic-v3-its.c    |  266 +++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/domain.h  |    2 +
 xen/include/asm-arm/gic-its.h |   49 ++++++++
 4 files changed, 318 insertions(+)
xen/arch/arm/Makefile +1 -0
index 1821ed2..8590846 100644
@@ -33,6 +33,7 @@ obj-y += traps.o
 obj-y += vgic.o vgic-v2.o
 obj-$(CONFIG_ARM_64) += vgic-v3.o
 obj-$(CONFIG_ARM_64) += gic-v3-its.o
+obj-$(CONFIG_ARM_64) += vgic-v3-its.o
 obj-y += vtimer.o
 obj-y += vuart.o
 obj-y += hvm.o
xen/arch/arm/vgic-v3-its.c new file +266 -0

266 lines not on this page · raw message

xen/include/asm-arm/domain.h +2 -0
index f1a087e..da73cf5 100644
@@ -115,6 +115,8 @@ struct arch_domain
 #endif
     } vgic;
 
+    struct vgic_its *vits;
+
     struct vuart {
 #define VUART_BUF_SIZE 128
         char                        *buf;
xen/include/asm-arm/gic-its.h +49 -0

61 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#41 lore raw

Re: [RFC PATCH v3 08/18] xen/arm: vITS: Add virtual ITS driver

Hi Vijay,

Quoted 22 lines · On 22/06/15 13:01, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

This patch introduces virtual ITS driver with following functionality
- Introduces helper functions to manage device table and ITT table in guest memory
- Helper function to handle virtual ITS devices assigned to domain

---
 xen/arch/arm/Makefile         |    1 +
 xen/arch/arm/vgic-v3-its.c    |  266 +++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/domain.h  |    2 +
 xen/include/asm-arm/gic-its.h |   49 ++++++++
 4 files changed, 318 insertions(+)
xen/arch/arm/Makefile +0 -0
index 1821ed2..8590846 100644

xen/arch/arm/Makefile obj-y += traps.o · line 36

Quoted 4 lines
xen/arch/arm/Makefile +1 -0
@@ -33,6 +33,7 @@ obj-y += traps.o
 obj-y += vgic.o vgic-v2.o
 obj-$(CONFIG_ARM_64) += vgic-v3.o
 obj-$(CONFIG_ARM_64) += gic-v3-its.o
+obj-$(CONFIG_ARM_64) += vgic-v3-its.o

I would prefer to the vgic-v3-its code not compiled until you finish to implement it rather than having every function exported while it should be static.

Quoted 8 lines
xen/arch/arm/Makefile +0 -0
 obj-y += vtimer.o
 obj-y += vuart.o
 obj-y += hvm.o
xen/arch/arm/vgic-v3-its.c new file +0 -0
new file mode 100644
index 0000000..ea52a87

[..]

xen/arch/arm/vgic-v3-its.c line 35

Quoted 2 lines
xen/arch/arm/vgic-v3-its.c new file +2 -0
+/* GITS register definitions */
+#define VITS_GITS_TYPER_HCC       (0xffU << 24)

Where does this value comes from? The number of supported collection should be the maximum number of VCPUs supported by the domain.

Note that the hardware can only hold 256 collections, all the other requires provisioning. But it's fine for now, although a BUILD_BUG_ON in the vGIC code would be nice.

xen/arch/arm/vgic-v3-its.c line 38

Quoted 3 lines
xen/arch/arm/vgic-v3-its.c new file +3 -0
+#define VITS_GITS_TYPER_PTA_SHIFT (19)
+#define VITS_GITS_DEV_BITS        (0x14U << 13)
+#define VITS_GITS_ID_BITS         (0x13U << 8)

Where do those values come from? This needs at least to come from the hardware GIC configuration if we plan to only support DOM0 for now.

Quoted 1 line

+#define VITS_GITS_ITT_SIZE (0x7U << 4)

The name is misleading, this is not the size of the ITT but the value to store in the GITS_TYPER register.

Quoted 1 line

+#define VITS_GITS_DISTRIBUTED (0x1U << 3)

Why? This bit is implementation defined in the spec.

Quoted 1 line

+#define VITS_GITS_PLPIS (0x1U << 0)

For all these defines, why do you hardcode the shift rather using GITS_TYPER_* you provide in patch #5? It would make the code more easier to understand.

Quoted 17 lines
xen/arch/arm/vgic-v3-its.c new file +17 -0
+
+/* GITS_PIDRn register values for ARM implementations */
+#define GITS_PIDR0_VAL            (0x94)
+#define GITS_PIDR1_VAL            (0xb4)
+#define GITS_PIDR2_VAL            (0x3b)
+#define GITS_PIDR3_VAL            (0x00)
+#define GITS_PIDR4_VAL            (0x04)
+
+// #define DEBUG_ITS
+
+#ifdef DEBUG_ITS
+# define DPRINTK(fmt, args...) dprintk(XENLOG_DEBUG, fmt, ##args)
+#else
+# define DPRINTK(fmt, args...) do {} while ( 0 )
+#endif
+
+#ifdef DEBUG_ITS

xen/arch/arm/vgic-v3-its.c line 68

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c new file +10 -0
+static void dump_cmd(its_cmd_block *cmd)
+{
+    printk("CMD[0] = 0x%lx CMD[1] = 0x%lx CMD[2] = 0x%lx CMD[3] = 0x%lx\n",
+           cmd->raw_cmd[0], cmd->raw_cmd[1], cmd->raw_cmd[2], cmd->raw_cmd[3]);
+}
+#endif
+
+/* ITS device table helper functions */
+int vits_vdevice_entry(struct domain *d, uint32_t dev_id,
+                       struct vdevice_table *entry, int set)

Any functions not exported should be static. I won't repeat for all the others within this file.

Also, please use bool_t for set.

xen/arch/arm/vgic-v3-its.c line 77

Quoted 9 lines
xen/arch/arm/vgic-v3-its.c new file +9 -0
+{
+    uint64_t offset;
+    paddr_t dt_entry;
+    struct page_info *page;
+    p2m_type_t p2mt;
+    void *p;
+
+    offset = dev_id * sizeof(struct vdevice_table);
+    if ( offset > d->arch.vits->dt_size )

If you gonna let the guest to write deci

xen/arch/arm/vgic-v3-its.c line 80

Quoted 3 lines
xen/arch/arm/vgic-v3-its.c new file +3 -0
+    {
+        dprintk(XENLOG_G_ERR,
+                "vITS:d%dv%d: Out of range offset 0x%lx id 0x%x size 0x%lx\n",

You can use %pv to print the domain/vcpuid (see an example in vgic-v3.c).

Also, all the value can be printed in decimal because we have to use them for addition...

Quoted 1 line

+ d->domain_id, current->vcpu_id, offset, dev_id,

Please don't mix d and current.

Quoted 11 lines
xen/arch/arm/vgic-v3-its.c new file +11 -0
+                d->arch.vits->dt_size);
+        return -EINVAL;
+    }
+
+    dt_entry = d->arch.vits->dt_ipa + offset;
+
+    page = get_page_from_gfn(d, paddr_to_pfn(dt_entry), &p2mt, P2M_ALLOC);
+    if ( !page )
+    {
+        dprintk(XENLOG_G_ERR, "vITS:d%dv%d Failed to get VITT device table\n",
+                d->domain_id, current->vcpu_id);

xen/arch/arm/vgic-v3-its.c line 102

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c new file +10 -0
+        return -EINVAL;
+    }
+
+    if ( !p2m_is_ram(p2mt) )
+    {
+        put_page(page);
+        dprintk(XENLOG_G_ERR, "vITS:d%dv%d with wrong attributes\n",
+                d->domain_id, current->vcpu_id);
+        return -EINVAL;
+        return -EINVAL;

Duplicate "return -EINVAL".

Quoted 73 lines
xen/arch/arm/vgic-v3-its.c new file +73 -0
+    }
+
+    p = __map_domain_page(page);
+    /* Offset within the mapped page */
+    offset = dt_entry & (PAGE_SIZE - 1);
+
+    if ( set )
+        memcpy(p + offset, entry, sizeof(struct vdevice_table));
+    else
+        memcpy(entry, p + offset, sizeof(struct vdevice_table));
+
+    unmap_domain_page(p);
+    put_page(page);
+
+    return 0;
+}
+
+int vits_set_vdevice_entry(struct domain *d, uint32_t devid,
+                           struct vdevice_table *entry)
+{
+    return vits_vdevice_entry(d, devid, entry, 1);  
+}
+
+int vits_get_vdevice_entry(struct domain *d, uint32_t devid,
+                           struct vdevice_table *entry)
+{
+    return vits_vdevice_entry(d, devid, entry, 0);  
+}
+
+int vits_vitt_entry(struct domain *d, uint32_t devid,
+                    uint32_t event, struct vitt *entry, int set)
+{
+    struct vdevice_table dt_entry;
+    struct page_info *page;
+    paddr_t vitt_entry;
+    p2m_type_t p2mt;
+    uint64_t offset;
+    void *p;
+
+    if ( vits_get_vdevice_entry(d, devid, &dt_entry) )
+    {
+        dprintk(XENLOG_G_ERR, "vITS:d%dv%d Fail to get vdevice for dev 0x%x\n",
+                d->domain_id, current->vcpu_id, devid);
+        return -EINVAL;
+    }
+
+    /* dt_entry is validated when read */
+    offset = event * sizeof(struct vitt);
+    if ( offset > dt_entry.vitt_size )
+    {
+        dprintk(XENLOG_G_ERR, "vITS:d%dv%d ITT out of range\n",
+                d->domain_id, current->vcpu_id);
+        return -EINVAL;
+    }
+   
+    vitt_entry = dt_entry.vitt_ipa + offset;
+    page = get_page_from_gfn(d, paddr_to_pfn(vitt_entry), &p2mt, P2M_ALLOC);
+    if ( !page )
+    {
+        dprintk(XENLOG_G_ERR, "vITS:d%dv%d Failed to get VITT device table\n",
+                d->domain_id, current->vcpu_id);
+        return -EINVAL;
+    }
+
+    if ( !p2m_is_ram(p2mt) )
+    {
+        put_page(page);
+        dprintk(XENLOG_G_ERR, "vITS:d%dv%d with wrong attributes\n",
+                d->domain_id, current->vcpu_id);
+        return -EINVAL;
+    }
+
+    p = __map_domain_page(page);

xen/arch/arm/vgic-v3-its.c line 185

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c new file +10 -0
+    /* Offset within the mapped page */
+    offset = vitt_entry & (PAGE_SIZE - 1);
+
+    if ( set )
+        memcpy(p + offset, entry, sizeof(struct vitt));
+    else
+        memcpy(entry, p + offset, sizeof(struct vitt));
+
+    unmap_domain_page(p);
+    put_page(page);

The code to transfer the data is exactly the same as vits_vdevice_entry except the size of the copy. Can you make an helper? It would avoid duplicate code.

Quoted 8 lines
xen/arch/arm/vgic-v3-its.c new file +8 -0
+
+    return 0;
+}
+
+int vits_set_vitt_entry(struct domain *d, uint32_t devid,
+                        uint32_t event, struct vitt *entry)
+{
+    return vits_vitt_entry(d, devid, event, entry, 1);  

xen/arch/arm/vgic-v3-its.c line 203

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c new file +10 -0
+}
+
+int vits_get_vitt_entry(struct domain *d, uint32_t devid,
+                        uint32_t event, struct vitt *entry)
+{
+    return vits_vitt_entry(d, devid, event, entry, 0);
+}
+
+/* RB-tree helpers for vits_device attached to a domain */
+struct vits_device * find_vits_device(struct rb_root *root, uint32_t devid)

Coding style: vits_device *find_

Quoted 38 lines
xen/arch/arm/vgic-v3-its.c new file +38 -0
+{
+    struct rb_node *node = root->rb_node;
+
+    while ( node )
+    {
+        struct vits_device *dev;
+
+        dev = container_of(node, struct vits_device, node);
+
+        if ( devid < dev->vdevid )
+            node = node->rb_left;
+        else if ( devid > dev->vdevid )
+            node = node->rb_right;
+        else
+            return dev;
+    }
+
+    return NULL;
+}
+
+int insert_vits_device(struct rb_root *root, struct vits_device *dev)
+{
+    struct rb_node **new, *parent;
+
+    new = &root->rb_node;
+    parent = NULL;
+    while ( *new )
+    {
+        struct vits_device *this;
+
+        this  = container_of(*new, struct vits_device, node);
+
+        parent = *new;
+        if ( dev->vdevid < this->vdevid )
+            new = &((*new)->rb_left);
+        else if ( dev->vdevid > this->vdevid )
+            new = &((*new)->rb_right);
+        else

xen/arch/arm/vgic-v3-its.c line 251

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c new file +10 -0
+            return -EEXIST;
+    }
+
+    rb_link_node(&dev->node, parent, new);
+    rb_insert_color(&dev->node, root);
+
+    return 0;
+}
+
+int remove_vits_device(struct rb_root *root, struct vits_device *dev)

This function can be void.

Quoted 33 lines
xen/arch/arm/vgic-v3-its.c new file +15 -0
+{
+    if ( dev )
+        rb_erase(&dev->node, root);
+
+    return 0;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/include/asm-arm/domain.h +2 -0
index f1a087e..da73cf5 100644
@@ -115,6 +115,8 @@ struct arch_domain
 #endif
     } vgic;
 
+    struct vgic_its *vits;
+
     struct vuart {
 #define VUART_BUF_SIZE 128
         char                        *buf;
xen/include/asm-arm/gic-its.h +0 -0
index a47cf26..a1099a1 100644

xen/include/asm-arm/gic-its.h struct its_collection { · line 36

Quoted 6 lines
xen/include/asm-arm/gic-its.h +3 -0
@@ -31,6 +31,35 @@ struct its_collection {
     u16 col_id;
 };
 
+/*
+ * Per domain virtual ITS structure.
+ * One per Physical ITS node available for the domain

Left over of the previous version? AFAICT, the agreed design [1] only expose one vITS per domain.

Quoted 6 lines
xen/include/asm-arm/gic-its.h +6 -0
+ */
+struct vgic_its
+{
+   spinlock_t lock;
+   /* Command queue base */
+   paddr_t cmd_base;

xen/include/asm-arm/gic-its.h struct its_collection { · line 52

Quoted 10 lines
xen/include/asm-arm/gic-its.h +10 -0
+   /* Command queue write pointer */
+   paddr_t cmd_write;
+   /* Command queue write saved pointer */
+   paddr_t cmd_write_save;
+   /* Command queue read pointer */
+   paddr_t cmd_read;
+   /* Command queue size */
+   unsigned long cmd_qsize;
+   /* ITS physical node */
+   struct its_node *its;

This is not necessary.

Quoted 14 lines
xen/include/asm-arm/gic-its.h +10 -0
+   /* vITT device table ipa */
+   paddr_t dt_ipa;
+   /* vITT device table size */
+   uint64_t dt_size;
+   /* Radix-tree root of devices attached to this domain */
+   struct rb_root dev_root;
+   /* collections mapped */
+   struct its_collection *collections;
+};
+
 /* ITS command structures */
 typedef struct __packed {
     u64 cmd:8;

xen/include/asm-arm/gic-its.h struct its_device { · line 237

Quoted 9 lines
xen/include/asm-arm/gic-its.h +6 -0
@@ -200,6 +229,26 @@ struct its_device {
     struct rb_node          node;
 };
 
+struct vits_device {
+    uint32_t vdevid;
+    uint32_t pdevid;
+    struct its_device *its_dev;
+    struct rb_node node;
+};

We spoke about a specific structure in the design [2] but you introduced a new one. Why?

Having everything in the its_device would help to catch a device attached to 2 different domains...

Also, the field pdevid is not vits specific but its.

xen/include/asm-arm/gic-its.h struct its_device { · line 250

Quoted 13 lines
xen/include/asm-arm/gic-its.h +13 -0
+
+struct vdevice_table {
+    uint64_t vitt_ipa;
+    uint32_t vitt_size;
+    uint32_t padding;
+};
+
+struct vitt {
+    uint16_t valid:1;
+    uint16_t pad:15;
+    uint16_t vcollection;
+    uint32_t vlpi;
+};

You forgot to add the BUILD_BUG_ON for each structure here. It's very important as you decided to hardcoded the value in the registers.

xen/include/asm-arm/gic-its.h struct its_device { · line 254

Quoted 5 lines
xen/include/asm-arm/gic-its.h +1 -0
+
 static inline uint8_t its_decode_cmd(its_cmd_block *cmd)
 {
     return cmd->raw_cmd[0] & 0xff;

Regards,

[1]
http://xenbits.xensource.com/people/ianc/vits/draftF.html#vits-to-pits-mapping
[2]
http://xenbits.xensource.com/people/ianc/vits/draftF.html#vits-to-pits-mapping

Signature · 1 line
Julien Grall
#42 lore raw

Re: [RFC PATCH v3 08/18] xen/arm: vITS: Add virtual ITS driver

Hi Julien,

Quoted 12 lines · Julien Grall

Hi Vijay,

xen/include/asm-arm/gic-its.h struct its_device { · line 237

Quoted 7 lines
xen/include/asm-arm/gic-its.h +6 -0
+struct vits_device {
+    uint32_t vdevid;
+    uint32_t pdevid;
+    struct its_device *its_dev;
+    struct rb_node node;
+};

We spoke about a specific structure in the design [2] but you introduced a new one. Why?

Section 6 of DraftG specifies to manage separate tree for device assignment. This helps to manage RB-tree per domain to hold list of devices assigned to this domain index with vdevid.

This helps to check if device is assigned to this domain before processing any ITS command with that vdevid.

Quoted 3 lines

Having everything in the its_device would help to catch a device attached to 2 different domains...

One option is to introduce a new variable inside its_device to know to which domain the device is currently assigned.

Quoted 2 lines

Also, the field pdevid is not vits specific but its.

pdevid can be removed as its_device structure already has it

Regards
Vijay

#43 lore raw

Re: [RFC PATCH v3 08/18] xen/arm: vITS: Add virtual ITS driver

Quoted 24 lines · Vijay Kilari

Hi Julien,

Quoted 12 lines · Julien Grall

Hi Vijay,

xen/include/asm-arm/gic-its.h struct its_device { · line 237

Quoted 7 lines
xen/include/asm-arm/gic-its.h +6 -0
+struct vits_device {
+    uint32_t vdevid;
+    uint32_t pdevid;
+    struct its_device *its_dev;
+    struct rb_node node;
+};

We spoke about a specific structure in the design [2] but you introduced a new one. Why?

Section 6 of DraftG specifies to manage separate tree for device assignment. This helps to manage RB-tree per domain to hold list of devices assigned to this domain index with vdevid.

This helps to check if device is assigned to this domain before processing any ITS command with that vdevid.

Quoted 1 line
Quoted 5 lines
Quoted 2 lines

Having everything in the its_device would help to catch a device attached to 2 different domains...

One option is to introduce a new variable inside its_device to know to which domain the device is currently assigned.

IIRC that's what I intended, e.g. two trees referencing the same underlying data structure. Sorry that wasn't clear.

Quoted 7 lines
Quoted 3 lines

Also, the field pdevid is not vits specific but its.

pdevid can be removed as its_device structure already has it

Regards
Vijay

#44 lore raw

Re: [RFC PATCH v3 08/18] xen/arm: vITS: Add virtual ITS driver

In reply to [email protected]

Hi,

xen/include/asm-arm/domain.h struct arch_domain · line 119

Quoted 10 lines · On 22/06/15 13:01, [email protected] wrote:
xen/include/asm-arm/domain.h +2 -0
index f1a087e..da73cf5 100644
@@ -115,6 +115,8 @@ struct arch_domain
 #endif
     } vgic;
 
+    struct vgic_its *vits;
+

I forgot to mention that this should only be compiled when GICv3 is used (i.e only for ARM64).

Regards,

Signature · 1 line
Julien Grall
#45 lore raw

Re: [RFC PATCH v3 08/18] xen/arm: vITS: Add virtual ITS driver

In reply to [email protected]

xen/arch/arm/vgic-v3-its.c line 100

Quoted 5 lines · On Mon, 2015-06-22 at 17:31 +0530, [email protected] wrote:
xen/arch/arm/vgic-v3-its.c new file +5 -0
+    if ( !p2m_is_ram(p2mt) )
+    {
+        put_page(page);
+        dprintk(XENLOG_G_ERR, "vITS:d%dv%d with wrong attributes\n",
+                d->domain_id, current->vcpu_id);

"d%dv%d" should be done (everywhere) with the "%pv" format code passing current as the argument. See docs/misc/printk-formats.txt.

xen/arch/arm/vgic-v3-its.c line 107

Quoted 7 lines
xen/arch/arm/vgic-v3-its.c new file +7 -0
+        return -EINVAL;
+        return -EINVAL;
+    }
+
+    p = __map_domain_page(page);
+    /* Offset within the mapped page */
+    offset = dt_entry & (PAGE_SIZE - 1);

This should be "& ~PAGE_MASK" please.

xen/arch/arm/vgic-v3-its.c line 112

Quoted 5 lines
xen/arch/arm/vgic-v3-its.c new file +5 -0
+
+    if ( set )
+        memcpy(p + offset, entry, sizeof(struct vdevice_table));
+    else
+        memcpy(entry, p + offset, sizeof(struct vdevice_table));

Personally I'd have done this with *entry = *(struct ..*)(p + offset) and vice versa and let the compiler figure out how to achieve that.

Quoted 1 line

+ /* dt_entry is validated when read */

The vits_get_vdevice_entry call is right above and in this implementation I don't see any checks in that function, so I don't think this comment is strictly true.

Since it is very important that information living in this guest accessible memory is correctly validated before use I think these comments need to be 100% trustworthy.

I think vits_get_vdevice_entry is indeed the right place for those checks as the comment suggests.

If there are no actual checks needed (e.g. because everything is in terms of IPA) then there should be a comment in that function explaining exactly why nothing actually needs to be validated.

Quoted 6 lines
xen/arch/arm/vgic-v3-its.c new file +6 -0
+    offset = event * sizeof(struct vitt);
+    if ( offset > dt_entry.vitt_size )
+    {
+        dprintk(XENLOG_G_ERR, "vITS:d%dv%d ITT out of range\n",
+                d->domain_id, current->vcpu_id);
+        return -EINVAL;
#46 lore raw

RFC PATCH v3 9/18 xen/arm: ITS: Add virtual ITS commands support

In reply to [email protected]

From: Vijaya Kumar K <[email protected]>

Add Virtual ITS command processing support to Virtual ITS driver

---
 xen/arch/arm/gic-v3-its.c  |    7 +
 xen/arch/arm/vgic-v3-its.c |  393 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 400 insertions(+)
xen/arch/arm/gic-v3-its.c +7 -0
index 535fc53..2a4fa97 100644
@@ -89,6 +89,7 @@ struct its_node {
 
 #define ITS_ITT_ALIGN    SZ_256
 
+static u32 id_bits;
 static LIST_HEAD(its_nodes);
 static DEFINE_SPINLOCK(its_lock);
 static struct rdist_prop  *gic_rdists;
@@ -146,6 +147,11 @@ void dump_cmd(its_cmd_block *cmd)
 }
 #endif
 
+u32 its_get_nr_events(void)
+{
+    return (1 << id_bits);
+}
+
 /* RB-tree helpers for its_device */
 struct its_device * find_its_device(struct rb_root *root, u32 devid)
 {
@@ -1044,6 +1050,7 @@ static int its_probe(struct dt_device_node *node)
     its->phys_size = its_size;
     typer = readl_relaxed(its_base + GITS_TYPER);
     its->ite_size = ((typer >> 4) & 0xf) + 1;
+    id_bits = GITS_TYPER_IDBITS(typer);
 
     its->cmd_base = xzalloc_bytes(ITS_CMD_QUEUE_SZ);
     if ( !its->cmd_base )
xen/arch/arm/vgic-v3-its.c +393 -0

399 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#47 lore raw

Re: [RFC PATCH v3 09/18] xen/arm: ITS: Add virtual ITS commands support

Hi Vijay,

Quoted 47 lines · On 22/06/15 13:01, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

Add Virtual ITS command processing support to Virtual ITS driver

---
 xen/arch/arm/gic-v3-its.c  |    7 +
 xen/arch/arm/vgic-v3-its.c |  393 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 400 insertions(+)
xen/arch/arm/gic-v3-its.c +7 -0
index 535fc53..2a4fa97 100644
@@ -89,6 +89,7 @@ struct its_node {
 
 #define ITS_ITT_ALIGN    SZ_256
 
+static u32 id_bits;
 static LIST_HEAD(its_nodes);
 static DEFINE_SPINLOCK(its_lock);
 static struct rdist_prop  *gic_rdists;
@@ -146,6 +147,11 @@ void dump_cmd(its_cmd_block *cmd)
 }
 #endif
 
+u32 its_get_nr_events(void)
+{
+    return (1 << id_bits);
+}
+
 /* RB-tree helpers for its_device */
 struct its_device * find_its_device(struct rb_root *root, u32 devid)
 {
@@ -1044,6 +1050,7 @@ static int its_probe(struct dt_device_node *node)
     its->phys_size = its_size;
     typer = readl_relaxed(its_base + GITS_TYPER);
     its->ite_size = ((typer >> 4) & 0xf) + 1;
+    id_bits = GITS_TYPER_IDBITS(typer);
 
     its->cmd_base = xzalloc_bytes(ITS_CMD_QUEUE_SZ);
     if ( !its->cmd_base )
xen/arch/arm/vgic-v3-its.c +0 -0
index ea52a87..0671434 100644

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 260

Quoted 5 lines
xen/arch/arm/vgic-v3-its.c +2 -0
@@ -256,6 +256,399 @@ int remove_vits_device(struct rb_root *root, struct vits_device *dev)
     return 0;
 }
 
+static int vgic_its_process_sync(struct vcpu *v, struct vgic_its *vits,
+                                 its_cmd_block *virt_cmd)

virt_cmd is not modified, please use const.

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 262

Quoted 2 lines
xen/arch/arm/vgic-v3-its.c +2 -0
+{
+    /* XXX: Ignored */

IHMO, XXX means TODO which is not the case here.

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 264

Quoted 2 lines
xen/arch/arm/vgic-v3-its.c +2 -0
+    DPRINTK("vITS:d%dv%d SYNC: ta 0x%x \n",
+             v->domain->domain_id, v->vcpu_id, virt_cmd->sync.ta);

You can use %pv rather than d%dv%d an directly pass the vcpu.

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 270

Quoted 6 lines
xen/arch/arm/vgic-v3-its.c +6 -0
+
+    return 0;
+}
+
+static int vgic_its_process_mapvi(struct vcpu *v, struct vgic_its *vits,
+                                  its_cmd_block *virt_cmd)

Please use const for the virt_cmd.

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 275

Quoted 5 lines
xen/arch/arm/vgic-v3-its.c +5 -0
+{
+    struct vitt entry;
+    struct vits_device *vdev;
+    uint8_t vcol_id, cmd;
+    uint32_t vid, dev_id, event;

struct domain *d = v->domain for a better abstraction in the code.

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 279

Quoted 4 lines
xen/arch/arm/vgic-v3-its.c +4 -0
+
+    vcol_id = virt_cmd->mapvi.col;
+    vid = virt_cmd->mapvi.phy_id;
+    dev_id = its_decode_devid(v->domain, virt_cmd);

AFAIU, the its_decode_devid will return a physical devID... although you function find_vits_device is working on virtual devID. This will also not work on fake device. Did I miss something?

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 285

Quoted 6 lines
xen/arch/arm/vgic-v3-its.c +6 -0
+    cmd = virt_cmd->mapvi.cmd;
+
+    DPRINTK("vITS:d%dv%d MAPVI: dev_id 0x%x vcol_id %d vid %d \n",
+             v->domain->domain_id, v->vcpu_id, dev_id, vcol_id, vid);
+
+    if ( vcol_id > (v->domain->max_vcpus + 1) ||  vid > its_get_nr_events() )

Checking the validity is pointless as a malicious guest can rewrite the ITT. We only need to check it when the LPI is effectively injected.

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 288

Quoted 3 lines
xen/arch/arm/vgic-v3-its.c +3 -0
+        return -EINVAL;
+
+    /* XXX: Enable validation later */

What do you mean?

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 291

Quoted 3 lines
xen/arch/arm/vgic-v3-its.c +3 -0
+    vdev = find_vits_device(&v->domain->arch.vits->dev_root, dev_id);
+    if ( !vdev && !vdev->its_dev )
+        return -EINVAL;

You deny the possibility to have fake device in the domain.

Anyway, this check is not necessary too.

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 302

Quoted 11 lines
xen/arch/arm/vgic-v3-its.c +11 -0
+
+    entry.valid = true;
+    entry.vcollection = vcol_id;
+    entry.vlpi = vid;
+
+    if ( cmd == GITS_CMD_MAPI )
+        vits_set_vitt_entry(v->domain, dev_id, vid, &entry);
+    else
+    {
+        event = virt_cmd->mapvi.event;
+        if ( event > its_get_nr_events() )

You have hardcoded the number of event in the vGIC but you are using the physical ITS to check the value.

IHMO, we should introduce a new field in the vITS to specify the number of events supported by the domain. For DOM0 it will be equal to the physical ITS.

But this check is also unnecessary.

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 312

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+            return -EINVAL;
+
+        vits_set_vitt_entry(v->domain, dev_id, event, &entry);
+    }
+
+    return 0;
+}
+
+static int vgic_its_process_movi(struct vcpu *v, struct vgic_its *vits,
+                                 its_cmd_block *virt_cmd)

virt_cmd is not modified, please use const.

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 317

Quoted 5 lines
xen/arch/arm/vgic-v3-its.c +5 -0
+{
+    struct vitt entry;
+    struct vits_device *vdev;
+    uint32_t dev_id, event;
+    uint8_t vcol_id;

struct domain *d = v->domain for a better abstraction in the code.

Quoted 4 lines
xen/arch/arm/vgic-v3-its.c +4 -0
+
+    dev_id = its_decode_devid(v->domain, virt_cmd);
+    vcol_id = virt_cmd->movi.col;
+    event = virt_cmd->movi.event;

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 331

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+
+    DPRINTK("vITS:d%dv%d MOVI: dev_id 0x%x vcol_id %d event %d\n",
+            v->domain->domain_id, v->vcpu_id, dev_id, vcol_id, event);
+    if ( vcol_id > (v->domain->max_vcpus + 1)  || event > its_get_nr_events() )
+        return -EINVAL;
+
+    /* Enable validation later when device assignment is done */
+    vdev = find_vits_device(&v->domain->arch.vits->dev_root, dev_id);
+    if ( !vdev && !vdev->its_dev )
+        return -EINVAL;

You deny the possibility to create the fake device. The only things you care is dev_id is within the limit of devID supported by the domain. I think the below helper take care of it.

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 344

Quoted 13 lines
xen/arch/arm/vgic-v3-its.c +13 -0
+
+    if ( vits_get_vitt_entry(v->domain, dev_id, event, &entry) )
+        return -EINVAL;
+    entry.vcollection = vcol_id;
+    if ( vits_set_vitt_entry(v->domain, dev_id, event, &entry) )
+        return -EINVAL;
+
+    return 0;
+}
+   
+static int vgic_its_process_discard(struct vcpu *v, struct vgic_its *vits,
+                                    its_cmd_block *virt_cmd)
+{

Most of my comments on the previous functions are valid within vgic_its_process_discard.

Quoted 19 lines
xen/arch/arm/vgic-v3-its.c +19 -0
+    struct vits_device *vdev;
+    struct vitt entry;
+    uint32_t event, dev_id;
+
+    event = virt_cmd->discard.event;
+    dev_id = its_decode_devid(v->domain, virt_cmd);
+
+    DPRINTK("vITS:d%dv%d DISCARD: dev_id 0x%x id %d\n",
+            v->domain->domain_id, v->vcpu_id, dev_id, event);
+    if ( event > its_get_nr_events() )
+        return -EINVAL;
+
+    /* Validated later */
+    vdev = find_vits_device(&v->domain->arch.vits->dev_root, dev_id);
+    if ( !vdev && !vdev->its_dev )
+        return -EINVAL;
+
+    if ( vits_get_vitt_entry(v->domain, dev_id, event, &entry) )
+        return -EINVAL;

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 373

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+    entry.valid = false;
+    if ( vits_set_vitt_entry(v->domain, dev_id, event, &entry) )
+        return -EINVAL;
+
+    return 0;
+}
+
+static int vgic_its_process_inv(struct vcpu *v, struct vgic_its *vits,
+                                its_cmd_block *virt_cmd)
+{

ditto

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 384

Quoted 11 lines
xen/arch/arm/vgic-v3-its.c +11 -0
+    /* XXX: Ignored */
+    DPRINTK("vITS:d%dv%d INV: dev_id 0x%x id %d\n",
+            v->domain->domain_id, v->vcpu_id, virt_cmd->inv.devid,
+            virt_cmd->inv.event);
+
+    return 0;
+}
+
+static int vgic_its_process_clear(struct vcpu *v, struct vgic_its *vits,
+                                  its_cmd_block *virt_cmd)
+{

ditto

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 395

Quoted 11 lines
xen/arch/arm/vgic-v3-its.c +11 -0
+    /* XXX: Ignored */
+    DPRINTK("vITS:d%dv%d CLEAR: dev_id 0x%x id %d\n",
+             v->domain->domain_id, v->vcpu_id, virt_cmd->clear.devid,
+             virt_cmd->clear.event);
+
+    return 0;
+}
+
+static int vgic_its_process_invall(struct vcpu *v, struct vgic_its *vits,
+                                   its_cmd_block *virt_cmd)
+{

ditto

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 405

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+    /* XXX: Ignored */
+    DPRINTK("vITS:d%dv%d INVALL: vCID %d\n",
+            v->domain->domain_id, v->vcpu_id, virt_cmd->invall.col);
+
+    return 0;
+}
+
+static int vgic_its_process_int(struct vcpu *v, struct vgic_its *vits,
+                                its_cmd_block *virt_cmd)
+{

ditto

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 407

Quoted 2 lines
xen/arch/arm/vgic-v3-its.c +2 -0
+    struct vitt vitt_entry;
+    uint32_t event, dev_id, col_id;

Newline after the declaration of the variable.

Quoted 11 lines
xen/arch/arm/vgic-v3-its.c +11 -0
+    event = virt_cmd->int_cmd.cmd;
+    dev_id = its_decode_devid(v->domain, virt_cmd);
+
+    DPRINTK("vITS:d%dv%d INT: Device 0x%x id %d\n",
+            v->domain->domain_id, v->vcpu_id, dev_id, event);
+    if ( event > its_get_nr_events() )
+        return -EINVAL;
+
+    if ( vits_get_vitt_entry(v->domain, dev_id, event, &vitt_entry) )
+        return -EINVAL;
+

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 428

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+    if ( !vitt_entry.valid )
+    {
+        dprintk(XENLOG_G_ERR,
+                "vITS:d%dv%d INT CMD invalid, event %d for dev 0x%x\n",
+                v->domain->domain_id, v->vcpu_id, event, dev_id);
+        return -EINVAL;
+    }
+
+    col_id = vitt_entry.vcollection;
+    ASSERT(col_id < v->domain->max_vcpus);

This need a proper check, a malicious guest can rewrite the VITT and crash Xen.

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 430

Quoted 2 lines
xen/arch/arm/vgic-v3-its.c +2 -0
+
+    vgic_vcpu_inject_irq(v->domain->vcpu[col_id], vitt_entry.vlpi);

On the draft [1], the implementation suggest for INT was to call vgic_vcpu_inject_lpi. Is there any issue to do it?

Also, you have to translate the col_id into to a VCPU ID.

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 436

Quoted 6 lines
xen/arch/arm/vgic-v3-its.c +6 -0
+
+    return 0;
+}
+
+static int vgic_its_add_device(struct vcpu *v, struct vgic_its *vits,
+                               its_cmd_block *virt_cmd)

Most of my comment on the previous functions can apply here.

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 445

Quoted 9 lines
xen/arch/arm/vgic-v3-its.c +9 -0
+{
+    struct domain *d = v->domain;
+    struct vdevice_table dt_entry;
+    uint32_t devid = its_decode_devid(v->domain, virt_cmd);
+
+    DPRINTK("vITS:d%dv%dd Add device dev_id 0x%x vitt_ipa = 0x%lx size %d\n",
+            v->domain->domain_id, v->vcpu_id, devid, (u64)virt_cmd->mapd.itt << 8,
+            virt_cmd->mapd.size);
+    /* XXX: Validate devid with physical ITS driver ? */

No need.

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 451

Quoted 6 lines
xen/arch/arm/vgic-v3-its.c +6 -0
+    if ( virt_cmd->mapd.valid )
+    {
+        /* itt field is 40 bit. extract 48 bit address by shifting */
+        dt_entry.vitt_ipa = virt_cmd->mapd.itt << 8;
+
+        dt_entry.vitt_size = (1 << (virt_cmd->mapd.size + 1)) * 8;

Where does come from the 8? Is it the size of the ITT? If so, you should use the proper define.

Quoted 50 lines
xen/arch/arm/vgic-v3-its.c +50 -0
+    }
+    else
+    {
+        dt_entry.vitt_ipa = INVALID_PADDR;
+        dt_entry.vitt_size = 0;
+    }
+
+    if ( vits_set_vdevice_entry(d, devid, &dt_entry) )
+        return -EINVAL;
+
+    return 0;
+}
+
+static int vgic_its_process_mapc(struct vcpu *v, struct vgic_its *vits,
+                                 its_cmd_block *virt_cmd)
+{
+    uint8_t vcol_id;
+    uint64_t vta = 0;
+
+    vcol_id = virt_cmd->mapc.col;
+    vta = virt_cmd->mapc.ta;
+
+    DPRINTK("vITS:d%dv%d MAPC: vCID %d vTA 0x%lx valid %d \n",
+            v->domain->domain_id, v->vcpu_id, vcol_id, vta,
+            virt_cmd->mapc.valid);
+    if ( vcol_id > (v->domain->max_vcpus + 1) || vta > v->domain->max_vcpus )
+        return -EINVAL;
+
+    if ( virt_cmd->mapc.valid )
+        v->domain->arch.vits->collections[vcol_id].target_address = vta;
+    else
+        v->domain->arch.vits->collections[vcol_id].target_address = ~0UL;
+
+    return 0;
+}
+
+static void vgic_its_update_read_ptr(struct vcpu *v, struct vgic_its *vits)
+{
+    vits->cmd_read = vits->cmd_write;
+}
+
+#ifdef DEBUG_ITS
+char *cmd_str[] = {
+        [GITS_CMD_MOVI]    = "MOVI",
+        [GITS_CMD_INT]     = "INT",
+        [GITS_CMD_CLEAR]   = "CLEAR",
+        [GITS_CMD_SYNC]    = "SYNC",
+        [GITS_CMD_MAPD]    = "MAPD",
+        [GITS_CMD_MAPC]    = "MAPC",
+        [GITS_CMD_MAPVI]   = "MAPVI",

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 511

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+        [GITS_CMD_MAPI]    = "MAPI",
+        [GITS_CMD_INV]     = "INV",
+        [GITS_CMD_INVALL]  = "INVALL",
+        [GITS_CMD_MOVALL]  = "MOVALL",
+        [GITS_CMD_DISCARD] = "DISCARD",
+    };
+#endif
+
+static int vgic_its_parse_its_command(struct vcpu *v, struct vgic_its *vits,
+                                      its_cmd_block *virt_cmd)

const its_cmd_block *virt_cmd;

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 518

Quoted 7 lines
xen/arch/arm/vgic-v3-its.c +7 -0
+{
+    uint8_t cmd = its_decode_cmd(virt_cmd);
+    int ret;
+
+#ifdef DEBUG_ITS
+    DPRINTK("vITS:d%dv%d Received cmd %s (0x%x)\n",
+            v->domain->domain_id, v->vcpu_id, cmd_str[cmd], cmd);

%pv

Quoted 29 lines
xen/arch/arm/vgic-v3-its.c +29 -0
+    DPRINTK("Dump Virt cmd: ");
+    dump_cmd(virt_cmd);
+#endif
+
+    switch ( cmd )
+    {
+    case GITS_CMD_MAPD:
+        ret = vgic_its_add_device(v, vits, virt_cmd);
+        break;
+    case GITS_CMD_MAPC:
+        ret =  vgic_its_process_mapc(v, vits, virt_cmd);
+        break;
+    case GITS_CMD_MAPI:
+        /* MAPI is same as MAPVI */
+    case GITS_CMD_MAPVI:
+        ret = vgic_its_process_mapvi(v, vits, virt_cmd);
+        break;
+    case GITS_CMD_MOVI:
+        ret = vgic_its_process_movi(v, vits, virt_cmd);
+        break;
+    case GITS_CMD_DISCARD:
+        ret = vgic_its_process_discard(v, vits, virt_cmd);
+        break;
+    case GITS_CMD_INV:
+        ret = vgic_its_process_inv(v, vits, virt_cmd);
+        break;
+    case GITS_CMD_INVALL:
+        ret = vgic_its_process_invall(v, vits, virt_cmd);
+        break;

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 557

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+    case GITS_CMD_INT:
+        ret = vgic_its_process_int(v, vits, virt_cmd);
+        break;
+    case GITS_CMD_CLEAR:
+        ret = vgic_its_process_clear(v, vits, virt_cmd);
+        break;
+    case GITS_CMD_SYNC:
+        ret = vgic_its_process_sync(v, vits, virt_cmd);
+        break;
+        /*TODO:  GITS_CMD_MOVALL not implemented */

I think we have to ignore it as we do for SYNC, INV, INVALL.

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 567

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+    default:
+       dprintk(XENLOG_G_ERR, "vITS:d%dv%d Unhandled command cmd %d\n",
+               v->domain->domain_id, v->vcpu_id, cmd);
+       return 1;
+    }
+
+    if ( ret )
+    {
+       dprintk(XENLOG_G_ERR, "vITS:d%dv%d Failed to handle cmd %d\n",
+               v->domain->domain_id, v->vcpu_id, cmd);

%pv

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 574

Quoted 7 lines
xen/arch/arm/vgic-v3-its.c +7 -0
+       return 1;
+    }
+
+    return 0;
+}
+
+/* Called with its lock held */

Please add an ASSERT to validate this assumption

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 579

Quoted 5 lines
xen/arch/arm/vgic-v3-its.c +5 -0
+static int vgic_its_read_virt_cmd(struct vcpu *v,
+                                  struct vgic_its *vits,
+                                  its_cmd_block *virt_cmd)
+{
+    struct page_info * page;

struct page_info *page;

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 586

Quoted 7 lines
xen/arch/arm/vgic-v3-its.c +7 -0
+    uint64_t offset;
+    unsigned long maddr;
+    void *p;
+
+    /* CMD Q can be more than 1 page. Map only page that is required */
+    maddr = ((vits->cmd_base & 0xfffffffff000UL) +
+              vits->cmd_write_save) & PAGE_MASK;

Define a mask would make the code clearer.

Also I don't see any check on GITS_CBASER.Valid in this code and in the register emulation.

If GITS_CBASER.Valid = 0, command should be ignore.

Quoted 11 lines
xen/arch/arm/vgic-v3-its.c +11 -0
+
+    DPRINTK("vITS:d%dv%d Mapping CMD Q maddr 0x%lx write_save 0x%lx \n",
+            v->domain->domain_id, v->vcpu_id, maddr, vits->cmd_write_save);
+
+    page = get_page_from_gfn(v->domain, paddr_to_pfn(maddr), NULL, P2M_ALLOC);
+    if ( page == NULL )
+    {
+        dprintk(XENLOG_G_ERR, "vITS:d%dv%d Failed to get command page\n",
+                v->domain->domain_id, v->vcpu_id);
+        return -EINVAL;
+    }

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 607

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+
+    p = __map_domain_page(page);
+
+    /* Offset within the mapped 4K page to read */
+    offset = vits->cmd_write_save & 0xfff;
+
+    memcpy(virt_cmd, p + offset, sizeof(its_cmd_block));
+
+    /* No command queue is created by vits to check on Q full */
+    vits->cmd_write_save += 0x20;

0x20 is confusing. I would use sizeof (struct its_cmd_block) or add a command to explain the 0x20.

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 617

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+    if ( vits->cmd_write_save == vits->cmd_qsize )
+    {
+         DPRINTK("vITS:d%dv%d Reset write_save 0x%lx qsize 0x%lx \n",
+                 v->domain->domain_id, v->vcpu_id, vits->cmd_write_save,
+                 vits->cmd_qsize);
+                 vits->cmd_write_save = 0x0;
+    }
+
+    unmap_domain_page(p);
+    put_page(page);

As suggested above, I would create an helper to read/write based on an IPA.

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 622

Quoted 5 lines
xen/arch/arm/vgic-v3-its.c +5 -0
+
+    return 0;
+}
+
+int vgic_its_process_cmd(struct vcpu *v, struct vgic_its *vits)

Missing a static

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 626

Quoted 4 lines
xen/arch/arm/vgic-v3-its.c +4 -0
+{
+    its_cmd_block virt_cmd;
+
+    /* XXX: Currently we are processing one cmd at a time */

This comment seems wrong. You are handling multiple commands at the same time.

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 636

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+    ASSERT(spin_is_locked(&vits->lock));
+
+    do {
+        if ( vgic_its_read_virt_cmd(v, vits, &virt_cmd) )
+            goto err;
+        if ( vgic_its_parse_its_command(v, vits, &virt_cmd) )
+            goto err;
+    } while ( vits->cmd_write != vits->cmd_write_save );
+
+    vits->cmd_write_save = vits->cmd_write;

Why this line? At the end of the loop cmd_write_save is equals to write_save. And technically, vits->cmd_write should be updated after each command.

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 641

Quoted 5 lines
xen/arch/arm/vgic-v3-its.c +5 -0
+    DPRINTK("vITS:d%dv%d write_save 0x%lx write 0x%lx\n",
+            v->domain->domain_id, v->vcpu_id, vits->cmd_write_save,
+            vits->cmd_write);
+    /* XXX: Currently we are processing one cmd at a time */
+    vgic_its_update_read_ptr(v, vits);

Ditto for the command.

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 643

Quoted 2 lines
xen/arch/arm/vgic-v3-its.c +2 -0
+
+    dsb(ishst);

Why the dsb?

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 649

Quoted 6 lines
xen/arch/arm/vgic-v3-its.c +6 -0
+
+    return 1;
+err:
+    dprintk(XENLOG_G_ERR, "vITS:d%dv%d Failed to process guest cmd\n",
+            v->domain->domain_id, v->vcpu_id);
+    return 0;

returning 0 here means that we will inject a data abort to the guest pointing to GITS_CWRITER. Although I'm not sure this what we want.

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 654

Quoted 6 lines
xen/arch/arm/vgic-v3-its.c +2 -0
+}
+
 /*
  * Local variables:
  * mode: C

Regards,

[1]
http://xenbits.xensource.com/people/ianc/vits/draftF.html#int-generate-an-interrupt

Signature · 1 line
Julien Grall
#48 lore raw

Re: [RFC PATCH v3 09/18] xen/arm: ITS: Add virtual ITS commands support

Quoted 9 lines · Julien Grall

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 451

Quoted 6 lines
xen/arch/arm/vgic-v3-its.c +6 -0
+    if ( virt_cmd->mapd.valid )
+    {
+        /* itt field is 40 bit. extract 48 bit address by shifting */
+        dt_entry.vitt_ipa = virt_cmd->mapd.itt << 8;
+
+        dt_entry.vitt_size = (1 << (virt_cmd->mapd.size + 1)) * 8;

Where does come from the 8? Is it the size of the ITT? If so, you should use the proper define.

sizeof(struct vitt) is what was meant, I think?

Ian.

#49 lore raw

Re: [RFC PATCH v3 09/18] xen/arm: ITS: Add virtual ITS commands support

In reply to [email protected]

On Mon, 2015-06-22 at 17:31 +0530, [email protected] wrote:
[...]

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 291

Quoted 4 lines
xen/arch/arm/vgic-v3-its.c +4 -0
+    /* XXX: Enable validation later */
+    vdev = find_vits_device(&v->domain->arch.vits->dev_root, dev_id);
+    if ( !vdev && !vdev->its_dev )
+        return -EINVAL;

vdev is unused I think. Which is good because according to the draft spec it should never be needed here. I believe that should be the case for all vgic_its_process_*.

Avoiding this lookup was part of the point of the major rework done in draftC of the spec.

#50 lore raw

Re: [RFC PATCH v3 09/18] xen/arm: ITS: Add virtual ITS commands support

In reply to [email protected]

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 279

Quoted 11 lines · On Mon, 2015-06-22 at 17:31 +0530, [email protected] wrote:
xen/arch/arm/vgic-v3-its.c +11 -0
+static int vgic_its_process_mapvi(struct vcpu *v, struct vgic_its *vits,
+                                  its_cmd_block *virt_cmd)
+{
+    struct vitt entry;
+    struct vits_device *vdev;
+    uint8_t vcol_id, cmd;
+    uint32_t vid, dev_id, event;
+
+    vcol_id = virt_cmd->mapvi.col;
+    vid = virt_cmd->mapvi.phy_id;
+    dev_id = its_decode_devid(v->domain, virt_cmd);

If you used the "union its_cmd" I proposed earlier for the virt_cmd argument then this would just be "virt_command->mapvi.devid".

[...]

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 513

Quoted 4 lines
xen/arch/arm/vgic-v3-its.c +4 -0
+static int vgic_its_parse_its_command(struct vcpu *v, struct vgic_its *vits,
+                                      its_cmd_block *virt_cmd)
+{
+    uint8_t cmd = its_decode_cmd(virt_cmd);

and this would be cirt_cmd->hdr.cmd.

[...]

Quoted 1 line

+ memcpy(virt_cmd, p + offset, sizeof(its_cmd_block));

and this would be

	memcpy(&virt_cmd.bits[0], p+offset, sizeof(union its_cmd));

Ian.

#51 lore raw

Re: [RFC PATCH v3 09/18] xen/arm: ITS: Add virtual ITS commands support

Quoted 15 lines · Ian Campbell

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 279

Quoted 11 lines · On Mon, 2015-06-22 at 17:31 +0530, [email protected] wrote:
xen/arch/arm/vgic-v3-its.c +11 -0
+static int vgic_its_process_mapvi(struct vcpu *v, struct vgic_its *vits,
+                                  its_cmd_block *virt_cmd)
+{
+    struct vitt entry;
+    struct vits_device *vdev;
+    uint8_t vcol_id, cmd;
+    uint32_t vid, dev_id, event;
+
+    vcol_id = virt_cmd->mapvi.col;
+    vid = virt_cmd->mapvi.phy_id;
+    dev_id = its_decode_devid(v->domain, virt_cmd);

If you used the "union its_cmd" I proposed earlier for the virt_cmd argument then this would just be "virt_command->mapvi.devid".

its_decode_devid() is used to get physical devid for a given virtual device id. I have to use virt_command->mapvi.devid.

If vitt entries are made base on physical dev id then

- This helps where in if interrupt is received we can extract
physical dev id from
the its_device structure held by irq_desc. With this physical device id we can search vitt entry to get collection/vcpu and vlpi information.

However fake devices cannot have physical device id. We cannot search vitt
entries. Esp INT command has only virtual dev id. So I think this approach is not
viable.

I propose:
Manage vitt entries based on virtual dev id and manage domain specific
RB-tree (which holds list of devices assigned to domain) with physical devid.

Now on receiving interrupt, will search for RB-tree based on physical dev id and get virtual dev id. With virtual dev id search vitt entries to get vcpu/collection & vlpi
info.

Also for fake devices, we search vitt entries with vdevice id for INT command.

Any suggestions?

Regards
Vijay

#52 lore raw

Re: [RFC PATCH v3 09/18] xen/arm: ITS: Add virtual ITS commands support

Quoted 20 lines · Vijay Kilari
Quoted 15 lines · Ian Campbell

xen/arch/arm/vgic-v3-its.c int remove_vits_device(struct rb_root *root, struct vits_device *dev) · line 279

Quoted 11 lines · On Mon, 2015-06-22 at 17:31 +0530, [email protected] wrote:
xen/arch/arm/vgic-v3-its.c +11 -0
+static int vgic_its_process_mapvi(struct vcpu *v, struct vgic_its *vits,
+                                  its_cmd_block *virt_cmd)
+{
+    struct vitt entry;
+    struct vits_device *vdev;
+    uint8_t vcol_id, cmd;
+    uint32_t vid, dev_id, event;
+
+    vcol_id = virt_cmd->mapvi.col;
+    vid = virt_cmd->mapvi.phy_id;
+    dev_id = its_decode_devid(v->domain, virt_cmd);

If you used the "union its_cmd" I proposed earlier for the virt_cmd argument then this would just be "virt_command->mapvi.devid".

its_decode_devid() is used to get physical devid for a given virtual device id. I have to use virt_command->mapvi.devid.

Quoted 6 lines

If vitt entries are made base on physical dev id then

- This helps where in if interrupt is received we can extract
physical dev id from
the its_device structure held by irq_desc. With this physical device id we can search vitt entry to get collection/vcpu and vlpi information.

http://xenbits.xen.org/people/ianc/vits/draftG.html#virtual-lpi-injection shows that there is no need for the physical device id when handling an pLPI and turning it into a vPLI injection.

In the pLPI case the vlpi comes out of the irq_desc and vgic_vcpu_inject_lpi only requires a vpli, not physical, which it then uses as indexes into the per-domain irq_pending
(http://xenbits.xen.org/people/ianc/vits/draftG.html#per-domain-struct-pending_irq-for-vlpis)

See also
http://xenbits.xen.org/people/ianc/vits/draftG.html#mapvi-map-an-input-identifier-to-a-physical-interrupt-and-an-interrupt-collection.
which describes how a MAPVI from the guest needs to be handled.

Quoted 4 lines

However fake devices cannot have physical device id. We cannot search vitt
entries. Esp INT command has only virtual dev id. So I think this approach is not
viable.

As shown above vgic_vcpu_inject_lpi only takes a vpli param, so the fact there that there is no physical device id for an INT command on a fake command doesn't matter.

Quoted 8 lines

I propose:
Manage vitt entries based on virtual dev id and manage domain specific
RB-tree (which holds list of devices assigned to domain) with physical devid.

Now on receiving interrupt, will search for RB-tree based on physical dev id and get virtual dev id. With virtual dev id search vitt entries to get vcpu/collection & vlpi
info.

The design document should shows there is no need for this.

You are very welcome to propose an alternative design, but I'm afraid that in that case I'm going to have to ask you to produce a comprehensive document covering all aspects of your proposed design, either from scratch or by modifying the existing draft to account for the consequences of your proposed changes across the board, i.e. it needs remain a coherent design across the board after your changes.

The fact that you are proposing an RB-tree looked during interrupt however is a problematic aspect of your proposal, in the existing design has been arranged such that this is not necessary.

Quoted 6 lines

Also for fake devices, we search vitt entries with vdevice id for INT command.

Any suggestions?

Regards
Vijay

#53 lore raw

RFC PATCH v3 10/18 xen/arm: ITS: Add APIs to add and assign device

In reply to [email protected]

From: Vijaya Kumar K <[email protected]>

Add APIs to add devices to RB-tree, assign and remove devices to domain.

---
 xen/arch/arm/gic-v3-its.c     |  246 ++++++++++++++++++++++++++++++++++++++++-
 xen/include/asm-arm/gic-its.h |    4 +-
 2 files changed, 246 insertions(+), 4 deletions(-)
xen/arch/arm/gic-v3-its.c +243 -3
index 2a4fa97..4471669 100644
@@ -94,6 +94,7 @@ static LIST_HEAD(its_nodes);
 static DEFINE_SPINLOCK(its_lock);
 static struct rdist_prop  *gic_rdists;
 static struct rb_root rb_its_dev;
+static DEFINE_SPINLOCK(rb_its_dev_lock);
 
 #define gic_data_rdist()            (per_cpu(rdist, smp_processor_id()))
 #define gic_data_rdist_rd_base()    (per_cpu(rdist, smp_processor_id()).rbase)
@@ -152,6 +153,21 @@ u32 its_get_nr_events(void)
     return (1 << id_bits);
 }
 
+static struct its_node * its_get_phys_node(u32 dev_id)
+{
+    struct its_node *its;
+
+    /* TODO: For now return ITS0 node.
+     * Need Query PCI helper function to get on which
+     * ITS node the device is attached
+     */
+    list_for_each_entry(its, &its_nodes, entry)
+    {
+        return its;
+    }
+
+    return NULL;
+}
 /* RB-tree helpers for its_device */
 struct its_device * find_its_device(struct rb_root *root, u32 devid)
 {
@@ -459,7 +475,7 @@ void its_send_inv(struct its_device *dev, struct its_collection *col,
     its_send_single_command(dev->its, its_build_inv_cmd, &desc);
 }
 
-void its_send_mapd(struct its_device *dev, int valid)
+static void its_send_mapd(struct its_device *dev, int valid)
 {
     struct its_cmd_desc desc;
 
@@ -493,7 +509,7 @@ void its_send_mapvi(struct its_device *dev, struct its_collection *col,
     its_send_single_command(dev->its, its_build_mapvi_cmd, &desc);
 }
 
-void its_send_movi(struct its_device *dev, struct its_collection *col,
+static void its_send_movi(struct its_device *dev, struct its_collection *col,
                           u32 event)
 {
     struct its_cmd_desc desc;
@@ -596,7 +612,7 @@ int its_lpi_init(u32 id_bits)
     return 0;
 }
 
-unsigned long *its_lpi_alloc_chunks(int nirqs, int *base, int *nr_ids)
+static unsigned long *its_lpi_alloc_chunks(int nirqs, int *base, int *nr_ids)
 {
     unsigned long *bitmap = NULL;
     int chunk_id;
@@ -636,6 +652,230 @@ out:
     return bitmap;
 }
 
+static void its_lpi_free(unsigned long *bitmap, int base, int nr_ids)
+{
+    int lpi;
+
+    spin_lock(&lpi_lock);
+
+    for ( lpi = base; lpi < (base + nr_ids); lpi += IRQS_PER_CHUNK )
+    {
+        int chunk = its_lpi_to_chunk(lpi);
+
+        BUG_ON(chunk > lpi_chunks);
+        if ( test_bit(chunk, lpi_bitmap) )
+            clear_bit(chunk, lpi_bitmap);
+        else
+            its_err("Bad LPI chunk %d\n", chunk);
+    }
+
+    spin_unlock(&lpi_lock);
+
+    xfree(bitmap);
+}
+
+static int its_alloc_device_irq(struct its_device *dev, u32 *hwirq)
+{
+    int idx;
+
+    idx = find_first_zero_bit(dev->lpi_map, dev->nr_lpis);
+    if ( idx == dev->nr_lpis )
+        return -ENOSPC;
+
+    *hwirq = dev->lpi_base + idx;
+    set_bit(idx, dev->lpi_map);
+
+    return 0;
+}
+
+static u32 its_get_plpi(struct its_device *dev, u32 event)
+{
+    ASSERT(event < dev->nr_lpis);
+    return dev->lpi_base + event;
+}
+
+/* Device assignment. Should be called from pci_device_add */
+int its_add_device(struct domain *d, u32 devid)
+{
+    struct its_device *dev;
+    unsigned long *lpi_map;
+    void *itt;
+    int lpi_base, nr_lpis, sz;
+    u32 i, nr_ites, plpi, nr_cpus;
+    struct its_collection *col;
+
+    spin_lock(&rb_its_dev_lock);
+    dev = find_its_device(&rb_its_dev, devid);
+    if ( dev )
+    {
+        spin_unlock(&rb_its_dev_lock);
+        dprintk(XENLOG_G_ERR, "ITS:d%dv%d Device already exists dev 0x%x\n",
+                d->domain_id, current->vcpu_id, dev->device_id);
+        return -EEXIST;
+    }
+    spin_unlock(&rb_its_dev_lock);
+
+    DPRINTK("ITS:d%dv%d Add device devid 0x%x\n",
+            d->domain_id, current->vcpu_id, devid);
+
+    dev = xzalloc(struct its_device);
+    if ( dev == NULL )
+        return -ENOMEM;
+
+    dev->its = its_get_phys_node(devid);
+    /* TODO: Use pci helper to get nvecs */
+    nr_ites = 64;
+    sz = nr_ites * dev->its->ite_size;
+    sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
+    itt = xzalloc_bytes(sz);
+    if ( !itt )
+        return -ENOMEM;
+
+    lpi_map = its_lpi_alloc_chunks(nr_ites, &lpi_base, &nr_lpis);
+    if ( !lpi_map || (nr_lpis != nr_ites) )
+    {
+         xfree(dev);
+         xfree(itt);
+         xfree(lpi_map);
+         return -EINVAL;
+    }
+
+    dev->itt_addr = (u64)itt;
+    dev->nr_ites = nr_ites;
+    dev->lpi_map = lpi_map;
+    dev->lpi_base = lpi_base;
+    /* nr_lpis should be always equal to nvecs */
+    dev->nr_lpis = nr_lpis;
+    dev->device_id = devid;
+
+    DPRINTK("ITS:d%dv%d Adding Device with id 0x%x nvecs %d lpi_base 0x%x\n",
+            d->domain_id, current->vcpu_id, dev->device_id,
+            dev->nr_lpis, dev->lpi_base);
+
+    /* Map device to its ITT */
+    its_send_mapd(dev, 1);
+
+    nr_cpus = num_online_cpus();
+    for ( i = 0; i < nr_ites; i++ )
+    {
+        /* Reserve pLPI */
+        if ( its_alloc_device_irq(dev, &plpi) )
+            return -ENOSPC;
+
+        /* For each pLPI send MAPVI command */
+        col = &dev->its->collections[(i % nr_cpus)];
+        its_send_mapvi(dev, col, plpi, i);
+    }
+
+    spin_lock(&rb_its_dev_lock);
+    if ( insert_its_device(&rb_its_dev, dev) )
+    {
+        spin_unlock(&rb_its_dev_lock);
+        return -EINVAL;
+    }
+    spin_unlock(&rb_its_dev_lock);
+
+    return 0;
+}
+
+int its_assign_device(struct domain *d, u32 vdevid)
+{
+    struct its_device *pdev;
+    struct vits_device *vdev;
+    struct irq_desc *desc;
+    u32 plpi, i, pdevid;
+
+    DPRINTK("ITS:d%dv%d Assign request for virtual device 0x%x\n",
+            d->domain_id, current->vcpu_id, vdevid);
+    vdev = find_vits_device(&d->arch.vits->dev_root, vdevid);
+    if ( vdev )
+        return -ENODEV;
+
+    /* TODO: Use PCI helper to convert vDevid to pDevid  */
+    pdevid = vdevid;
+    spin_lock(&rb_its_dev_lock);
+    pdev = find_its_device(&rb_its_dev, pdevid);
+    if ( !pdev )
+    {
+        spin_unlock(&rb_its_dev_lock);
+        return -ENODEV;
+    }
+    spin_unlock(&rb_its_dev_lock);
+
+    vdev = xzalloc_bytes(sizeof(struct vits_device));
+    if ( !pdev )
+        return -ENOMEM;
+
+    vdev->its_dev = pdev;
+    vdev->vdevid = vdevid;
+    vdev->pdevid = pdevid;
+
+    /* Insert to domains' list */
+    if ( insert_vits_device(&d->arch.vits->dev_root, vdev) )
+        return -EINVAL;
+
+    DPRINTK("ITS:d%dv%d Assign pdevid 0x%x with nvecs %d\n",
+            d->domain_id, current->vcpu_id, pdev->device_id, pdev->nr_lpis);
+
+    /* XXX: Event id always starts from 0? */
+    for ( i = 0; i < pdev->nr_lpis; i++ )
+    {
+        plpi = its_get_plpi(pdev, i);
+        route_irq_to_guest(d, i, plpi, "LPI");
+        /* Enable all the event in LPI configuration table */
+        desc = irq_to_desc(plpi);
+        set_irq_device(desc, pdev);
+        lpi_set_config(desc, 1);
+    }
+
+    return 0;
+}
+
+int its_detach_device(struct domain *d, u32 vdevid)
+{
+    struct its_device *pdev;
+    struct vits_device *vdev;
+    struct irq_desc *desc;
+    u32 plpi, i, pdevid;
+
+    DPRINTK("ITS:d%dv%d Detach request for virtual device 0x%x\n",
+            d->domain_id, current->vcpu_id, vdevid);
+    vdev = find_vits_device(&d->arch.vits->dev_root, vdevid);
+    if ( !vdev )
+        return -EINVAL;
+
+    /* XXX: convert vDevid to pDevid ? */
+    pdevid = vdevid;
+    spin_lock(&rb_its_dev_lock);
+    pdev = find_its_device(&rb_its_dev, pdevid);
+    if ( !pdev )
+    {
+        spin_unlock(&rb_its_dev_lock);
+        return -EINVAL;
+    }
+    spin_unlock(&rb_its_dev_lock);
+
+    DPRINTK("ITS:d%dv%d Detach pdevid 0x%x with nvecs %d\n",
+            d->domain_id, current->vcpu_id, pdev->device_id, pdev->nr_lpis);
+    /* Remove vits_device from domain list */
+    remove_vits_device(&d->arch.vits->dev_root, vdev);
+    xfree(vdev);
+
+    for ( i = 0; i < pdev->nr_lpis; i++ )
+    {
+        plpi = its_get_plpi(pdev, i);
+        /* XXX: Event id always starts from 0 */
+        release_irq(plpi, d);
+        /* Disable all the event in LPI configuration table */
+        desc = irq_to_desc(plpi);
+        set_irq_device(desc, NULL);
+        lpi_set_config(desc, 0);
+    }
+
+    its_lpi_free(pdev->lpi_map, pdev->lpi_base, pdev->nr_lpis);
+
+    return 0;
+}
 /*
  * We allocate 64kB for PROPBASE. That gives us at most 64K LPIs to
  * deal with (one configuration byte per interrupt). PENDBASE has to
xen/include/asm-arm/gic-its.h +3 -1
index a1099a1..8f898a6 100644
@@ -263,7 +263,9 @@ static inline uint32_t its_decode_devid(struct domain *d, its_cmd_block *cmd)
 void its_set_affinity(struct irq_desc *desc, int cpu);
 void lpi_set_config(struct irq_desc *desc, int enable);
 uint32_t its_get_nr_events(void);
-
+struct vits_device * find_vits_device(struct rb_root *root, uint32_t devid);
+int insert_vits_device(struct rb_root *root, struct vits_device *dev);
+int remove_vits_device(struct rb_root *root, struct vits_device *dev);
 #endif /* __ASM_ARM_GIC_ITS_H__ */
 
 /*
Signature · 1 line
1.7.9.5
#54 lore raw

Re: [RFC PATCH v3 10/18] xen/arm: ITS: Add APIs to add and assign device

Hi Vijay,

Quoted 16 lines · On 22/06/15 13:01, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

Add APIs to add devices to RB-tree, assign and remove devices to domain.

---
 xen/arch/arm/gic-v3-its.c     |  246 ++++++++++++++++++++++++++++++++++++++++-
 xen/include/asm-arm/gic-its.h |    4 +-
 2 files changed, 246 insertions(+), 4 deletions(-)
xen/arch/arm/gic-v3-its.c +0 -0
index 2a4fa97..4471669 100644

xen/arch/arm/gic-v3-its.c static LIST_HEAD(its_nodes); · line 97

Quoted 4 lines
xen/arch/arm/gic-v3-its.c +1 -0
@@ -94,6 +94,7 @@ static LIST_HEAD(its_nodes);
 static DEFINE_SPINLOCK(its_lock);
 static struct rdist_prop  *gic_rdists;
 static struct rb_root rb_its_dev;
+static DEFINE_SPINLOCK(rb_its_dev_lock);

Can't you use the its_lock to handle locking for the rb_its_dev?

Quoted 26 lines
xen/arch/arm/gic-v3-its.c +15 -0
 #define gic_data_rdist()            (per_cpu(rdist, smp_processor_id()))
 #define gic_data_rdist_rd_base()    (per_cpu(rdist, smp_processor_id()).rbase)
@@ -152,6 +153,21 @@ u32 its_get_nr_events(void)
     return (1 << id_bits);
 }
 
+static struct its_node * its_get_phys_node(u32 dev_id)
+{
+    struct its_node *its;
+
+    /* TODO: For now return ITS0 node.
+     * Need Query PCI helper function to get on which
+     * ITS node the device is attached
+     */
+    list_for_each_entry(its, &its_nodes, entry)
+    {
+        return its;
+    }
+
+    return NULL;
+}
 /* RB-tree helpers for its_device */
 struct its_device * find_its_device(struct rb_root *root, u32 devid)
 {

xen/arch/arm/gic-v3-its.c void its_send_inv(struct its_device *dev, struct its_collection *col, · line 478

Quoted 5 lines
xen/arch/arm/gic-v3-its.c +1 -1
@@ -459,7 +475,7 @@ void its_send_inv(struct its_device *dev, struct its_collection *col,
     its_send_single_command(dev->its, its_build_inv_cmd, &desc);
 }
 
-void its_send_mapd(struct its_device *dev, int valid)
+static void its_send_mapd(struct its_device *dev, int valid)

I would prefer to see this static where the function has been introduced and delay the compilation until the end.

xen/arch/arm/gic-v3-its.c void its_send_mapvi(struct its_device *dev, struct its_collection *col, · line 513

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +1 -1
 {
     struct its_cmd_desc desc;
 
@@ -493,7 +509,7 @@ void its_send_mapvi(struct its_device *dev, struct its_collection *col,
     its_send_single_command(dev->its, its_build_mapvi_cmd, &desc);
 }
 
-void its_send_movi(struct its_device *dev, struct its_collection *col,
+static void its_send_movi(struct its_device *dev, struct its_collection *col,
                           u32 event)

Ditto

xen/arch/arm/gic-v3-its.c int its_lpi_init(u32 id_bits) · line 615

Quoted 8 lines
xen/arch/arm/gic-v3-its.c +1 -1
 {
     struct its_cmd_desc desc;
@@ -596,7 +612,7 @@ int its_lpi_init(u32 id_bits)
     return 0;
 }
 
-unsigned long *its_lpi_alloc_chunks(int nirqs, int *base, int *nr_ids)
+static unsigned long *its_lpi_alloc_chunks(int nirqs, int *base, int *nr_ids)

Ditto

Quoted 28 lines
xen/arch/arm/gic-v3-its.c +21 -0
 {
     unsigned long *bitmap = NULL;
     int chunk_id;
@@ -636,6 +652,230 @@ out:
     return bitmap;
 }
 
+static void its_lpi_free(unsigned long *bitmap, int base, int nr_ids)
+{
+    int lpi;
+
+    spin_lock(&lpi_lock);
+
+    for ( lpi = base; lpi < (base + nr_ids); lpi += IRQS_PER_CHUNK )
+    {
+        int chunk = its_lpi_to_chunk(lpi);
+
+        BUG_ON(chunk > lpi_chunks);
+        if ( test_bit(chunk, lpi_bitmap) )
+            clear_bit(chunk, lpi_bitmap);
+        else
+            its_err("Bad LPI chunk %d\n", chunk);
+    }
+
+    spin_unlock(&lpi_lock);
+
+    xfree(bitmap);
+}

xen/arch/arm/gic-v3-its.c out: · line 685

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+
+static int its_alloc_device_irq(struct its_device *dev, u32 *hwirq)
+{
+    int idx;
+
+    idx = find_first_zero_bit(dev->lpi_map, dev->nr_lpis);
+    if ( idx == dev->nr_lpis )
+        return -ENOSPC;
+
+    *hwirq = dev->lpi_base + idx; 

You could use its_get_plpi here.

xen/arch/arm/gic-v3-its.c out: · line 697

Quoted 12 lines
xen/arch/arm/gic-v3-its.c +12 -0
+    set_bit(idx, dev->lpi_map);
+
+    return 0;
+}
+
+static u32 its_get_plpi(struct its_device *dev, u32 event)
+{
+    ASSERT(event < dev->nr_lpis);
+    return dev->lpi_base + event;
+}
+
+/* Device assignment. Should be called from pci_device_add */

I guess you mean PHYSDEVOP_pci_device_add?

xen/arch/arm/gic-v3-its.c out: · line 698

Quoted 1 line
xen/arch/arm/gic-v3-its.c +1 -0
+int its_add_device(struct domain *d, u32 devid)

Why do you pass a domain in parameter? This function only adds a device into the ITS, there is nothing related to a specific domain.

For more abstraction, I would create an helper which take a struct device in parameter and retrieving all the necessary informations (devid, number of MSI...) and then call this function.

xen/arch/arm/gic-v3-its.c out: · line 709

Quoted 11 lines
xen/arch/arm/gic-v3-its.c +11 -0
+{
+    struct its_device *dev;
+    unsigned long *lpi_map;
+    void *itt;
+    int lpi_base, nr_lpis, sz;
+    u32 i, nr_ites, plpi, nr_cpus;
+    struct its_collection *col;
+
+    spin_lock(&rb_its_dev_lock);
+    dev = find_its_device(&rb_its_dev, devid);
+    if ( dev )

This check is not useful. As you release the lock before adding the device someone else can have time to add the device in the RB-tree.

Quoted 8 lines
xen/arch/arm/gic-v3-its.c +8 -0
+    {
+        spin_unlock(&rb_its_dev_lock);
+        dprintk(XENLOG_G_ERR, "ITS:d%dv%d Device already exists dev 0x%x\n",
+                d->domain_id, current->vcpu_id, dev->device_id);
+        return -EEXIST;
+    }
+    spin_unlock(&rb_its_dev_lock);
+

xen/arch/arm/gic-v3-its.c out: · line 727

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+    DPRINTK("ITS:d%dv%d Add device devid 0x%x\n",
+            d->domain_id, current->vcpu_id, devid);
+
+    dev = xzalloc(struct its_device);
+    if ( dev == NULL )
+        return -ENOMEM;
+
+    dev->its = its_get_phys_node(devid);
+    /* TODO: Use pci helper to get nvecs */
+    nr_ites = 64;

How did you choose this value?

xen/arch/arm/gic-v3-its.c out: · line 732

Quoted 5 lines
xen/arch/arm/gic-v3-its.c +5 -0
+    sz = nr_ites * dev->its->ite_size;
+    sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
+    itt = xzalloc_bytes(sz);
+    if ( !itt )
+        return -ENOMEM;

You forgot to free dev if we fail to allocate itt.

As Linux does, you can do the allocation check in one place. But you will have to create a local variable for the ITS.

xen/arch/arm/gic-v3-its.c out: · line 735

Quoted 3 lines
xen/arch/arm/gic-v3-its.c +3 -0
+
+    lpi_map = its_lpi_alloc_chunks(nr_ites, &lpi_base, &nr_lpis);
+    if ( !lpi_map || (nr_lpis != nr_ites) )

The check "nr_lpis != nr_ites" is here to ensure that its_lpi_alloc_chuncks allocate the right number of LPI, right? If so, given this is the only place you call its_lpi_alloc_chunks, why don't you put the check within the function? It would avoid one parameter.

xen/arch/arm/gic-v3-its.c out: · line 743

Quoted 8 lines
xen/arch/arm/gic-v3-its.c +8 -0
+    {
+         xfree(dev);
+         xfree(itt);
+         xfree(lpi_map);
+         return -EINVAL;
+    }
+
+    dev->itt_addr = (u64)itt;

The cast is not necessary.

xen/arch/arm/gic-v3-its.c out: · line 747

Quoted 4 lines
xen/arch/arm/gic-v3-its.c +4 -0
+    dev->nr_ites = nr_ites;
+    dev->lpi_map = lpi_map;
+    dev->lpi_base = lpi_base;
+    /* nr_lpis should be always equal to nvecs */

nvecs doesn't have any meaning within this function. Do you mean nr_ites?

Quoted 9 lines
xen/arch/arm/gic-v3-its.c +9 -0
+    dev->nr_lpis = nr_lpis;
+    dev->device_id = devid;
+
+    DPRINTK("ITS:d%dv%d Adding Device with id 0x%x nvecs %d lpi_base 0x%x\n",
+            d->domain_id, current->vcpu_id, dev->device_id,
+            dev->nr_lpis, dev->lpi_base);
+
+    /* Map device to its ITT */
+    its_send_mapd(dev, 1);

xen/arch/arm/gic-v3-its.c out: · line 766

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+
+    nr_cpus = num_online_cpus();
+    for ( i = 0; i < nr_ites; i++ )
+    {
+        /* Reserve pLPI */
+        if ( its_alloc_device_irq(dev, &plpi) )
+            return -ENOSPC;
+
+        /* For each pLPI send MAPVI command */
+        col = &dev->its->collections[(i % nr_cpus)];

Hmmmm.... In patch #7 you are using a different way to get the collection for the LPIs. Can you introduce an helper to get the collection?

xen/arch/arm/gic-v3-its.c out: · line 771

Quoted 5 lines
xen/arch/arm/gic-v3-its.c +5 -0
+        its_send_mapvi(dev, col, plpi, i);
+    }
+
+    spin_lock(&rb_its_dev_lock);
+    if ( insert_its_device(&rb_its_dev, dev) )

You have to undo the mapd, mapvi, and free all the allocated memory if the insertion fail.

You may want to rework the code in to avoid taking care of that.

Quoted 8 lines
xen/arch/arm/gic-v3-its.c +8 -0
+    {
+        spin_unlock(&rb_its_dev_lock);
+        return -EINVAL;
+    }
+    spin_unlock(&rb_its_dev_lock);
+
+    return 0;
+}

xen/arch/arm/gic-v3-its.c out: · line 789

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+
+int its_assign_device(struct domain *d, u32 vdevid)
+{
+    struct its_device *pdev;
+    struct vits_device *vdev;
+    struct irq_desc *desc;
+    u32 plpi, i, pdevid;
+
+    DPRINTK("ITS:d%dv%d Assign request for virtual device 0x%x\n",
+            d->domain_id, current->vcpu_id, vdevid);

Please don't mix d and current. The first one point to the domain where the device will be assigned, the latter is the vcpu which issue the hypercall to assign.

You don't need to print current here.

xen/arch/arm/gic-v3-its.c out: · line 792

Quoted 3 lines
xen/arch/arm/gic-v3-its.c +3 -0
+    vdev = find_vits_device(&d->arch.vits->dev_root, vdevid);
+    if ( vdev )
+        return -ENODEV;

ENODEV mean there is not device. In this case the vDevID already exists. You may want to use -EEXISTS.

xen/arch/arm/gic-v3-its.c out: · line 795

Quoted 3 lines
xen/arch/arm/gic-v3-its.c +3 -0
+
+    /* TODO: Use PCI helper to convert vDevid to pDevid  */
+    pdevid = vdevid;

How the PCI helper would convert a vDevID to pDevID? This is entirely up to the toolstack.

It would be preferrable that this function take a device and the vDevID in parameter.

xen/arch/arm/gic-v3-its.c out: · line 803

Quoted 8 lines
xen/arch/arm/gic-v3-its.c +8 -0
+    spin_lock(&rb_its_dev_lock);
+    pdev = find_its_device(&rb_its_dev, pdevid);
+    if ( !pdev )
+    {
+        spin_unlock(&rb_its_dev_lock);
+        return -ENODEV;
+    }
+    spin_unlock(&rb_its_dev_lock);

Same remark as in its_add_device for finding a device.

Quoted 5 lines
xen/arch/arm/gic-v3-its.c +5 -0
+
+    vdev = xzalloc_bytes(sizeof(struct vits_device));
+    if ( !pdev )
+        return -ENOMEM;
+

xen/arch/arm/gic-v3-its.c out: · line 818

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+    vdev->its_dev = pdev;
+    vdev->vdevid = vdevid;
+    vdev->pdevid = pdevid;
+
+    /* Insert to domains' list */
+    if ( insert_vits_device(&d->arch.vits->dev_root, vdev) )
+        return -EINVAL;
+
+    DPRINTK("ITS:d%dv%d Assign pdevid 0x%x with nvecs %d\n",
+            d->domain_id, current->vcpu_id, pdev->device_id, pdev->nr_lpis);

ditto for d and current

xen/arch/arm/gic-v3-its.c out: · line 828

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+
+    /* XXX: Event id always starts from 0? */
+    for ( i = 0; i < pdev->nr_lpis; i++ )
+    {
+        plpi = its_get_plpi(pdev, i);
+        route_irq_to_guest(d, i, plpi, "LPI");
+        /* Enable all the event in LPI configuration table */
+        desc = irq_to_desc(plpi);
+        set_irq_device(desc, pdev);
+        lpi_set_config(desc, 1);

route_irq_to_guest expects to see a vIRQ in parameter and not an eventID. Actually, all the check are based on this assumption (even after you changes in patch #16).

The correct solution is to introduce a new function to route the LPI and set correctly the required information.

Quoted 4 lines
xen/arch/arm/gic-v3-its.c +4 -0
+    }
+
+    return 0;
+}

xen/arch/arm/gic-v3-its.c out: · line 842

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+
+int its_detach_device(struct domain *d, u32 vdevid)
+{
+    struct its_device *pdev;
+    struct vits_device *vdev;
+    struct irq_desc *desc;
+    u32 plpi, i, pdevid;
+
+    DPRINTK("ITS:d%dv%d Detach request for virtual device 0x%x\n",
+            d->domain_id, current->vcpu_id, vdevid);

ditto for d and current.

Quoted 4 lines
xen/arch/arm/gic-v3-its.c +4 -0
+    vdev = find_vits_device(&d->arch.vits->dev_root, vdevid);
+    if ( !vdev )
+        return -EINVAL;
+

xen/arch/arm/gic-v3-its.c out: · line 856

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+    /* XXX: convert vDevid to pDevid ? */
+    pdevid = vdevid;
+    spin_lock(&rb_its_dev_lock);
+    pdev = find_its_device(&rb_its_dev, pdevid);
+    if ( !pdev )
+    {
+        spin_unlock(&rb_its_dev_lock);
+        return -EINVAL;
+    }
+    spin_unlock(&rb_its_dev_lock);

Why do you need this? You have the vDev which contains a pointer to the its_device structure.

Quoted 5 lines
xen/arch/arm/gic-v3-its.c +5 -0
+
+    DPRINTK("ITS:d%dv%d Detach pdevid 0x%x with nvecs %d\n",
+            d->domain_id, current->vcpu_id, pdev->device_id, pdev->nr_lpis);
+    /* Remove vits_device from domain list */
+    remove_vits_device(&d->arch.vits->dev_root, vdev);

xen/arch/arm/gic-v3-its.c out: · line 871

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+    xfree(vdev);
+
+    for ( i = 0; i < pdev->nr_lpis; i++ )
+    {
+        plpi = its_get_plpi(pdev, i);
+        /* XXX: Event id always starts from 0 */
+        release_irq(plpi, d);
+        /* Disable all the event in LPI configuration table */
+        desc = irq_to_desc(plpi);
+        set_irq_device(desc, NULL);

release_irq free irq_guest so this will segfault. As for route_irq_to_guest please introduce a function to release the LPI.

xen/arch/arm/gic-v3-its.c out: · line 872

Quoted 1 line
xen/arch/arm/gic-v3-its.c +1 -0
+        lpi_set_config(desc, 0);

The interrupt should be disabled before deassigning from the guest.

IHMO, all this stuff should be done the a new release function.

xen/arch/arm/gic-v3-its.c out: · line 875

Quoted 3 lines
xen/arch/arm/gic-v3-its.c +3 -0
+    }
+
+    its_lpi_free(pdev->lpi_map, pdev->lpi_base, pdev->nr_lpis);

Why do you free the physical LPI chunk? We want to be able to reassign the guest later.

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +2 -0
+    return 0;
+}
 /*
  * We allocate 64kB for PROPBASE. That gives us at most 64K LPIs to
  * deal with (one configuration byte per interrupt). PENDBASE has to
xen/include/asm-arm/gic-its.h +0 -0
index a1099a1..8f898a6 100644

xen/include/asm-arm/gic-its.h static inline uint32_t its_decode_devid(struct domain *d, its_cmd_block *cmd) · line 266

Quoted 5 lines
xen/include/asm-arm/gic-its.h +1 -1
@@ -263,7 +263,9 @@ static inline uint32_t its_decode_devid(struct domain *d, its_cmd_block *cmd)
 void its_set_affinity(struct irq_desc *desc, int cpu);
 void lpi_set_config(struct irq_desc *desc, int enable);
 uint32_t its_get_nr_events(void);
-
+struct vits_device * find_vits_device(struct rb_root *root, uint32_t devid);

Coding style. And in general, the function prototype should be declared in the patch where the function has been added not when it's used.

xen/include/asm-arm/gic-its.h static inline uint32_t its_decode_devid(struct domain *d, its_cmd_block *cmd) · line 271

Quoted 6 lines
xen/include/asm-arm/gic-its.h +2 -0
+int insert_vits_device(struct rb_root *root, struct vits_device *dev);
+int remove_vits_device(struct rb_root *root, struct vits_device *dev);
 #endif /* __ASM_ARM_GIC_ITS_H__ */
 
 /*

Regards,

Signature · 1 line
Julien Grall
#55 lore raw

Re: [RFC PATCH v3 10/18] xen/arm: ITS: Add APIs to add and assign device

On Wed, 2015-06-24 at 12:38 +0100, Julien Grall wrote:
[...]

Quoted 9 lines

xen/arch/arm/gic-v3-its.c void its_send_inv(struct its_device *dev, struct its_collection *col, · line 478

Quoted 6 lines
xen/arch/arm/gic-v3-its.c +1 -1
@@ -459,7 +475,7 @@ void its_send_inv(struct its_device *dev, struct its_collection *col,
     its_send_single_command(dev->its, its_build_inv_cmd, &desc);
 }
 
-void its_send_mapd(struct its_device *dev, int valid)
+static void its_send_mapd(struct its_device *dev, int valid)

I would prefer to see this static where the function has been introduced and delay the compilation until the end.

Yes, please.

#56 lore raw

Re: [RFC PATCH v3 10/18] xen/arm: ITS: Add APIs to add and assign device

In reply to [email protected]

xen/arch/arm/gic-v3-its.c out: · line 699

Quoted 3 lines · CIOn Mon, 2015-06-22 at 17:31 +0530, [email protected] wrote:
xen/arch/arm/gic-v3-its.c +3 -0
+/* Device assignment. Should be called from pci_device_add */
+int its_add_device(struct domain *d, u32 devid)
+{

Prior to the PCI series landing, and to enable dom0 to use ITS it might be possible to call this from xen/arch/arm/platforms/thunderx.c via the specific_mappings platform hook, which would also expose the PCI controller to dom0 via a series of specific mmio mappings (look at xen/arch/arm/platforms/xgene-storm.c for the sort of thing I mean).

That would, I think, give basic PCI functionality for dom0 (i.e. allowing us to boot on thunderx) and decouple things from the PCI series somewhat, which ought to make things easier overall IMHO.

Ian.

#57 lore raw

Re: [RFC PATCH v3 10/18] xen/arm: ITS: Add APIs to add and assign device

Quoted 11 lines · Ian Campbell

xen/arch/arm/gic-v3-its.c out: · line 699

Quoted 3 lines · CIOn Mon, 2015-06-22 at 17:31 +0530, [email protected] wrote:
xen/arch/arm/gic-v3-its.c +3 -0
+/* Device assignment. Should be called from pci_device_add */
+int its_add_device(struct domain *d, u32 devid)
+{

Prior to the PCI series landing, and to enable dom0 to use ITS it might be possible to call this from xen/arch/arm/platforms/thunderx.c via the specific_mappings platform hook, which would also expose the PCI controller to dom0 via a series of specific mmio mappings (look at xen/arch/arm/platforms/xgene-storm.c for the sort of thing I mean).

Quoted 3 lines

That would, I think, give basic PCI functionality for dom0 (i.e. allowing us to boot on thunderx) and decouple things from the PCI series somewhat, which ought to make things easier overall IMHO.

In case ThunderX, mmio mappings PCI RC is parsed to find devices.
How do we know device ids upfront to call its_add_device?

Quoted 3 lines

Ian.

#58 lore raw

Re: [RFC PATCH v3 10/18] xen/arm: ITS: Add APIs to add and assign device

Quoted 12 lines · Vijay Kilari
Quoted 11 lines · Ian Campbell

xen/arch/arm/gic-v3-its.c out: · line 699

Quoted 3 lines · CIOn Mon, 2015-06-22 at 17:31 +0530, [email protected] wrote:
xen/arch/arm/gic-v3-its.c +3 -0
+/* Device assignment. Should be called from pci_device_add */
+int its_add_device(struct domain *d, u32 devid)
+{

Prior to the PCI series landing, and to enable dom0 to use ITS it might be possible to call this from xen/arch/arm/platforms/thunderx.c via the specific_mappings platform hook, which would also expose the PCI controller to dom0 via a series of specific mmio mappings (look at xen/arch/arm/platforms/xgene-storm.c for the sort of thing I mean).

Quoted 6 lines
Quoted 3 lines

That would, I think, give basic PCI functionality for dom0 (i.e. allowing us to boot on thunderx) and decouple things from the PCI series somewhat, which ought to make things easier overall IMHO.

In case ThunderX, mmio mappings PCI RC is parsed to find devices.
How do we know device ids upfront to call its_add_device?

Ah yes, this approach probably doesn't work for ITS. Which is a shame.

Ian.

#59 lore raw

Re: [RFC PATCH v3 10/18] xen/arm: ITS: Add APIs to add and assign device

Quoted 13 lines · Ian Campbell
Quoted 12 lines · Vijay Kilari
Quoted 11 lines · Ian Campbell

xen/arch/arm/gic-v3-its.c out: · line 699

Quoted 3 lines · CIOn Mon, 2015-06-22 at 17:31 +0530, [email protected] wrote:
xen/arch/arm/gic-v3-its.c +3 -0
+/* Device assignment. Should be called from pci_device_add */
+int its_add_device(struct domain *d, u32 devid)
+{

Prior to the PCI series landing, and to enable dom0 to use ITS it might be possible to call this from xen/arch/arm/platforms/thunderx.c via the specific_mappings platform hook, which would also expose the PCI controller to dom0 via a series of specific mmio mappings (look at xen/arch/arm/platforms/xgene-storm.c for the sort of thing I mean).

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

That would, I think, give basic PCI functionality for dom0 (i.e. allowing us to boot on thunderx) and decouple things from the PCI series somewhat, which ought to make things easier overall IMHO.

In case ThunderX, mmio mappings PCI RC is parsed to find devices.
How do we know device ids upfront to call its_add_device?

Ah yes, this approach probably doesn't work for ITS. Which is a shame.

I guess the number of device IDs supported by thunderX is bounded and not so big. So you could do a loop which register all the IDs from 0 to N.

Though, it may be possible to be smarter.

Regards,

Signature · 1 line
Julien Grall
#60 lore raw

RFC PATCH v3 11/18 xen/arm: ITS: Add GITS registers emulation

In reply to [email protected]

From: Vijaya Kumar K <[email protected]>

Emulate GITS* registers and handle LPI configuration table update trap.

---
 xen/arch/arm/vgic-v3-its.c    |  516 +++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h |   14 ++
 2 files changed, 530 insertions(+)
xen/arch/arm/vgic-v3-its.c +516 -0
index 0671434..fa9dccc 100644
@@ -63,6 +63,46 @@ static void dump_cmd(its_cmd_block *cmd)
 }
 #endif
 
+void vgic_its_disable_lpis(struct vcpu *v, uint32_t vlpi)
+{
+    struct pending_irq *p;
+    unsigned long flags;
+
+    p = irq_to_pending(v, vlpi);
+    clear_bit(GIC_IRQ_GUEST_ENABLED, &p->status);
+    gic_remove_from_queues(v, vlpi);
+    if ( p->desc != NULL )
+    {
+        spin_lock_irqsave(&p->desc->lock, flags);
+        p->desc->handler->disable(p->desc);
+        spin_unlock_irqrestore(&p->desc->lock, flags);
+    }
+}
+
+void vgic_its_enable_lpis(struct vcpu *v, uint32_t vlpi, uint8_t priority)
+{
+    struct pending_irq *p;
+    unsigned long flags;
+
+    /* Get plpi for the given vlpi */
+    p = irq_to_pending(v, vlpi);
+    p->priority = priority;
+    set_bit(GIC_IRQ_GUEST_ENABLED, &p->status);
+
+    spin_lock_irqsave(&v->arch.vgic.lock, flags);
+
+    if ( !list_empty(&p->inflight) &&
+         !test_bit(GIC_IRQ_GUEST_VISIBLE, &p->status) )
+        gic_raise_guest_irq(v, irq_to_virq(p->desc), p->priority);
+
+    spin_unlock_irqrestore(&v->arch.vgic.lock, flags);
+    if ( p->desc != NULL )
+    {
+        spin_lock_irqsave(&p->desc->lock, flags);
+        p->desc->handler->enable(p->desc);
+        spin_unlock_irqrestore(&p->desc->lock, flags);
+    }
+}
 /* ITS device table helper functions */
 int vits_vdevice_entry(struct domain *d, uint32_t dev_id,
                        struct vdevice_table *entry, int set)
@@ -649,6 +689,482 @@ err:
     return 0;
 }
 
+static int vgic_v3_gits_lpi_mmio_read(struct vcpu *v, mmio_info_t *info)
+{
+    uint32_t offset;
+    struct hsr_dabt dabt = info->dabt;
+    struct cpu_user_regs *regs = guest_cpu_user_regs();
+    register_t *r = select_user_reg(regs, dabt.reg);
+    uint8_t cfg;
+
+    offset = info->gpa -
+             (v->domain->arch.vits->propbase & 0xfffffffff000UL);
+
+    if ( offset < SZ_64K )
+    {
+        DPRINTK("vITS:d%dv%d LPI Table read offset 0x%x\n",
+                v->domain->domain_id, v->vcpu_id, offset);
+        cfg = readb_relaxed(v->domain->arch.vits->prop_page + offset);
+        *r = cfg;
+        return 1;
+    }
+    else
+        dprintk(XENLOG_G_ERR, "vITS:d%dv%d LPI Table read with wrong offset 0x%x\n",
+                v->domain->domain_id, v->vcpu_id, offset);
+
+
+    return 0;
+}
+
+static int vgic_v3_gits_lpi_mmio_write(struct vcpu *v, mmio_info_t *info)
+{
+    uint32_t offset;
+    uint32_t vid;
+    uint8_t cfg;
+    bool_t enable;
+    struct hsr_dabt dabt = info->dabt;
+    struct cpu_user_regs *regs = guest_cpu_user_regs();
+    register_t *r = select_user_reg(regs, dabt.reg);
+
+    offset = info->gpa -
+             (v->domain->arch.vits->propbase & 0xfffffffff000UL);
+
+    vid = offset + NR_GIC_LPI;
+    if ( offset < SZ_64K )
+    {
+        DPRINTK("vITS:d%dv%d LPI Table write offset 0x%x\n",
+                v->domain->domain_id, v->vcpu_id, offset);
+        cfg = readb_relaxed(v->domain->arch.vits->prop_page + offset);
+        enable = (cfg & *r) & 0x1;
+
+        if ( !enable )
+             vgic_its_enable_lpis(v, vid,  (*r & 0xfc));
+        else
+             vgic_its_disable_lpis(v, vid);
+
+        /* Update virtual prop page */
+        writeb_relaxed((*r & 0xff),
+                        v->domain->arch.vits->prop_page + offset);
+        
+        return 1;
+    }
+    else
+        dprintk(XENLOG_G_ERR, "vITS:d%dv%d LPI Table invalid write @ 0x%x\n",
+                v->domain->domain_id, v->vcpu_id, offset);
+
+    return 0; 
+}
+
+static const struct mmio_handler_ops vgic_gits_lpi_mmio_handler = {
+    .read_handler  = vgic_v3_gits_lpi_mmio_read,
+    .write_handler = vgic_v3_gits_lpi_mmio_write,
+};
+
+int vgic_its_unmap_lpi_prop(struct vcpu *v)
+{
+    paddr_t maddr;
+    uint32_t lpi_size;
+    int i;
+    
+    maddr = v->domain->arch.vits->propbase & 0xfffffffff000UL;
+    lpi_size = 1UL << ((v->domain->arch.vits->propbase & 0x1f) + 1);
+
+    DPRINTK("vITS:d%dv%d Unmap guest LPI conf table maddr 0x%lx lpi_size 0x%x\n", 
+             v->domain->domain_id, v->vcpu_id, maddr, lpi_size);
+
+    if ( lpi_size < SZ_64K )
+    {
+        dprintk(XENLOG_G_ERR, "vITS:d%dv%d LPI Prop page < 64K\n",
+                v->domain->domain_id, v->vcpu_id);
+        return 0;
+    }
+
+    /* XXX: As per 4.8.9 each re-distributor shares a common LPI configuration table 
+     * So one set of mmio handlers to manage configuration table is enough
+     */
+    for ( i = 0; i < lpi_size / PAGE_SIZE; i++ )
+        guest_physmap_remove_page(v->domain, paddr_to_pfn(maddr),
+                                gmfn_to_mfn(v->domain, paddr_to_pfn(maddr)), 0);
+
+    /* Register mmio handlers for this region */
+    register_mmio_handler(v->domain, &vgic_gits_lpi_mmio_handler,
+                          maddr, lpi_size);
+
+    /* Allocate Virtual LPI Property table */
+    v->domain->arch.vits->prop_page =
+        alloc_xenheap_pages(get_order_from_bytes(lpi_size), 0);
+    if ( !v->domain->arch.vits->prop_page )
+    {
+        dprintk(XENLOG_G_ERR, "vITS:d%dv%d Failed to allocate LPI Prop page\n",
+                v->domain->domain_id, v->vcpu_id);
+        return 0;
+    }
+
+    memset(v->domain->arch.vits->prop_page, 0xa2, lpi_size);
+
+    return 1;
+}
+
+static inline void vits_spin_lock(struct vgic_its *vits)
+{
+    spin_lock(&vits->lock);
+}
+
+static inline void vits_spin_unlock(struct vgic_its *vits)
+{
+    spin_unlock(&vits->lock);
+}
+
+static int vgic_v3_gits_mmio_read(struct vcpu *v, mmio_info_t *info)
+{
+    struct vgic_its *vits;
+    struct hsr_dabt dabt = info->dabt;
+    struct cpu_user_regs *regs = guest_cpu_user_regs();
+    register_t *r = select_user_reg(regs, dabt.reg);
+    uint64_t val = 0;
+    uint32_t index, gits_reg;
+
+    vits = v->domain->arch.vits;
+
+    gits_reg = info->gpa - vits->phys_base;
+
+    if ( gits_reg >= SZ_64K )
+    {
+        gdprintk(XENLOG_G_WARNING,
+                 "vITS:d%dv%d unknown gpa read address %"PRIpaddr"\n",
+                 v->domain->domain_id, v->vcpu_id, info->gpa);
+        return 0;
+    }
+
+    switch ( gits_reg )
+    {
+    case GITS_CTLR:
+        if ( dabt.size != DABT_WORD )
+            goto bad_width;
+        *r = 0;
+        return 1;
+    case GITS_IIDR:
+        if ( dabt.size != DABT_WORD )
+            goto bad_width;
+        *r = 0;
+        return 1;
+    case GITS_TYPER:
+        vits_spin_lock(vits);
+        val = (((v->domain->max_vcpus + 1) << GITS_TYPER_HCC_SHIFT ) |
+                 VITS_GITS_DEV_BITS | VITS_GITS_ID_BITS              |
+                 VITS_GITS_ITT_SIZE | VITS_GITS_DISTRIBUTED          |
+                 VITS_GITS_PLPIS);
+        if ( dabt.size == DABT_DOUBLE_WORD )
+            *r = val;
+        else if ( dabt.size == DABT_WORD )
+            *r = (u32)val;
+        else
+        {
+            vits_spin_unlock(vits);
+            goto bad_width;
+        }
+        vits_spin_unlock(vits);
+        return 1;
+    case GITS_TYPER + 4:
+        if (dabt.size != DABT_WORD ) goto bad_width;
+        vits_spin_lock(vits);
+        val = (((v->domain->max_vcpus + 1) << GITS_TYPER_HCC_SHIFT ) |
+                 VITS_GITS_DEV_BITS | VITS_GITS_ID_BITS              |
+                 VITS_GITS_ITT_SIZE | VITS_GITS_DISTRIBUTED          |
+                 VITS_GITS_PLPIS);
+        *r = (u32)(val >> 32);
+        vits_spin_unlock(vits);
+        return 1;
+    case 0x0010 ... 0x007c:
+    case 0xc000 ... 0xffcc:
+        /* Implementation defined -- read ignored */
+        dprintk(XENLOG_ERR,
+                "vITS:d%dv%d read unknown 0x000c - 0x007c r%d offset %#08x\n",
+                v->domain->domain_id, v->vcpu_id, dabt.reg, gits_reg);
+        goto read_as_zero;
+    case GITS_CBASER:
+        /* XXX: Only read support 32/64-bit access */
+        vits_spin_lock(vits);
+        if ( dabt.size == DABT_DOUBLE_WORD )
+            *r = vits->cmd_base && 0xc7ffffffffffffffUL;
+        else if ( dabt.size == DABT_WORD )
+            *r = (u32)vits->cmd_base;
+        else
+        {
+            vits_spin_unlock(vits);
+            goto bad_width;
+        }
+        vits_spin_unlock(vits);
+        return 1;
+    case GITS_CBASER + 4:
+        if (dabt.size != DABT_WORD )
+            goto bad_width;
+        vits_spin_lock(vits);
+        *r = (u32)(vits->cmd_base >> 32);
+        vits_spin_unlock(vits);
+        return 1;
+    case GITS_CWRITER:
+        /* XXX: Only read support 32/64-bit access */
+        vits_spin_lock(vits);
+        if ( dabt.size == DABT_DOUBLE_WORD )
+            *r = vits->cmd_write;
+        else if ( dabt.size == DABT_WORD )
+            *r = (u32)vits->cmd_write;
+        else
+        {
+            vits_spin_unlock(vits);
+            goto bad_width;
+        }
+        vits_spin_unlock(vits);
+        return 1;
+    case GITS_CWRITER + 4:
+        if ( dabt.size != DABT_WORD )
+            goto bad_width;
+        vits_spin_lock(vits);
+        *r = (u32)(vits->cmd_write >> 32);
+        vits_spin_unlock(vits);
+        return 1;
+    case GITS_CREADR:
+        /* XXX: Only read support 32/64-bit access */
+        vits_spin_lock(vits);
+        if ( dabt.size == DABT_DOUBLE_WORD )
+            *r = vits->cmd_read;
+        else if ( dabt.size == DABT_WORD )
+            *r = (u32)vits->cmd_read;
+        else
+        {
+            vits_spin_unlock(vits);
+            goto bad_width;
+        }
+        vits_spin_unlock(vits);
+        return 1;
+    case GITS_CREADR + 4:
+        if ( dabt.size != DABT_WORD )
+            goto bad_width;
+        vits_spin_lock(vits);
+        *r = (u32)(vits->cmd_read >> 32);
+        vits_spin_unlock(vits);
+        return 1;
+    case 0x0098 ... 0x009c:
+    case 0x00a0 ... 0x00fc:
+    case 0x0140 ... 0xbffc:
+        /* Reserved -- read ignored */
+        dprintk(XENLOG_ERR,
+                "vITS:d%dv%d read unknown 0x0098-9c or 0x00a0-fc r%d offset %#08x\n",
+                v->domain->domain_id, v->vcpu_id, dabt.reg, gits_reg);
+        goto read_as_zero;
+    case GITS_BASER ... GITS_BASERN:
+        /* Supports only 64-bit access */
+        if ( dabt.size != DABT_DOUBLE_WORD )
+            goto bad_width;
+        if ( (gits_reg % 8) != 0 )
+            goto bad_width;
+        vits_spin_lock(vits);
+        index = (gits_reg - GITS_BASER) / 8;
+        *r = vits->baser[index];
+        vits_spin_unlock(vits);
+        return 1;
+    case GITS_PIDR0:
+        if ( dabt.size != DABT_WORD )
+            goto bad_width;
+        *r = GITS_PIDR0_VAL;
+        return 1;
+    case GITS_PIDR1:
+        if ( dabt.size != DABT_WORD )
+            goto bad_width;
+        *r = GITS_PIDR1_VAL;
+        return 1;
+    case GITS_PIDR2:
+        if ( dabt.size != DABT_WORD )
+            goto bad_width;
+        *r = GITS_PIDR2_VAL;
+        return 1;
+    case GITS_PIDR3:
+        if ( dabt.size != DABT_WORD )
+            goto bad_width;
+        *r = GITS_PIDR3_VAL;
+        return 1;
+    case GITS_PIDR4:
+        if ( dabt.size != DABT_WORD )
+            goto bad_width;
+        *r = GITS_PIDR4_VAL;
+        return 1;
+    case GITS_PIDR5 ... GITS_PIDR7:
+        goto read_as_zero;
+   default:
+        dprintk(XENLOG_G_ERR, "vITS:d%dv%d unhandled read r%d offset %#08x\n",
+                v->domain->domain_id, v->vcpu_id, dabt.reg, gits_reg);
+        return 0;
+    }
+
+bad_width:
+    dprintk(XENLOG_G_ERR, "vITS:d%dv%d bad read width %d r%d offset %#08x\n",
+           v->domain->domain_id, v->vcpu_id, dabt.size, dabt.reg, gits_reg);
+    domain_crash_synchronous();
+    return 0;
+
+read_as_zero:
+    if ( dabt.size != DABT_WORD )
+       goto bad_width;
+    *r = 0;
+    return 1;
+}
+
+#define GITS_BASER_MASK  (~((0x7UL << GITS_BASER_TYPE_SHIFT) | \
+                         (0xffUL << GITS_BASER_ENTRY_SIZE_SHIFT) | \
+                         (0x3UL << GITS_BASER_SHAREABILITY_SHIFT)))
+
+static int vgic_v3_gits_mmio_write(struct vcpu *v, mmio_info_t *info)
+{
+    struct vgic_its *vits;
+    struct hsr_dabt dabt = info->dabt;
+    struct cpu_user_regs *regs = guest_cpu_user_regs();
+    register_t *r = select_user_reg(regs, dabt.reg);
+    int ret;
+    uint32_t index, gits_reg, sz, psz;
+    uint64_t val;
+
+    vits = v->domain->arch.vits;
+
+    gits_reg = info->gpa - vits->phys_base;
+
+    if ( gits_reg >= SZ_64K )
+    {
+        gdprintk(XENLOG_G_WARNING,
+                 "vITS:d%dv%d unknown gpa write address %"PRIpaddr"\n",
+                 v->domain->domain_id, v->vcpu_id, info->gpa);
+        return 0;
+    }
+    switch ( gits_reg )
+    {
+    case GITS_CTLR:
+        if ( dabt.size != DABT_WORD )
+            goto bad_width;
+        vits_spin_lock(vits);
+        vits->ctrl = *r;
+        vits_spin_unlock(vits);
+        return 1;
+    case GITS_IIDR:
+        /* R0 -- write ignored */
+        goto write_ignore;
+    case GITS_TYPER:
+    case GITS_TYPER + 4:
+        /* R0 -- write ignored */
+        goto write_ignore;
+    case 0x0010 ... 0x007c:
+    case 0xc000 ... 0xffcc:
+        /* Implementation defined -- write ignored */
+        dprintk(XENLOG_G_ERR,
+                "vITS:d%dv%d write to unknown 0x000c - 0x007c r%d offset %#08x\n",
+                v->domain->domain_id, v->vcpu_id, dabt.reg, gits_reg);
+        goto write_ignore;
+    case GITS_CBASER:
+        if ( dabt.size != DABT_DOUBLE_WORD )
+            goto bad_width;
+        vits_spin_lock(vits);
+        vits->cmd_base = *r;
+        vits->cmd_qsize  =  SZ_4K * ((*r & 0xff) + 1);
+        vits_spin_unlock(vits);
+        return 1;
+    case GITS_CBASER + 4:
+         /* XXX: Does not support word write */
+        goto bad_width;
+    case GITS_CWRITER:
+        vits_spin_lock(vits);
+        if ( dabt.size == DABT_DOUBLE_WORD )
+            vits->cmd_write = *r;
+        else if ( dabt.size == DABT_WORD)
+        {
+            val = vits->cmd_write & 0xffffffff00000000UL;
+            val = (*r) | val;
+            vits->cmd_write =  val;
+        }
+        else
+        {
+            vits_spin_unlock(vits);
+            goto bad_width;
+        }
+        ret = vgic_its_process_cmd(v, vits);
+        vits_spin_unlock(vits);
+        return ret;
+    case GITS_CWRITER + 4:
+        if (dabt.size != DABT_WORD )
+            goto bad_width;
+        vits_spin_lock(vits);
+        val = vits->cmd_write & 0xffffffffUL;
+        val = ((*r & 0xffffffffUL) << 32) | val;
+        vits->cmd_write =  val;
+        ret = vgic_its_process_cmd(v, vits);
+        vits_spin_unlock(vits);
+        return ret;
+    case GITS_CREADR:
+        /* R0 -- write ignored */
+        goto write_ignore;
+    case 0x0098 ... 0x009c:
+    case 0x00a0 ... 0x00fc:
+    case 0x0140 ... 0xbffc:
+        /* Reserved -- write ignored */
+        dprintk(XENLOG_G_ERR,
+                "vITS:d%dv%d write to unknown 0x98-9c or 0xa0-fc r%d offset %#08x\n",
+                v->domain->domain_id, v->vcpu_id, dabt.reg, gits_reg);
+        goto write_ignore;
+    case GITS_BASER0:
+        if ( dabt.size != DABT_DOUBLE_WORD )
+            goto bad_width;
+        vits_spin_lock(vits);
+        vits->baser[0] = vits->baser[0] | (GITS_BASER_MASK & *r);
+        vits->dt_ipa = vits->baser[0] & 0xfffffffff000UL;
+        psz = (vits->baser[0] >> GITS_BASER_PAGE_SIZE_SHIFT) &
+               GITS_BASER_PAGE_SIZE_MASK_VAL;
+        if ( psz == GITS_BASER_PAGE_SIZE_4K_VAL )
+            sz = 4;
+        else if ( psz == GITS_BASER_PAGE_SIZE_16K_VAL )
+            sz = 16;
+        else
+            sz = 64;
+
+        vits->dt_size = (vits->baser[0] & GITS_BASER_PAGES_MASK_VAL)
+                        * sz * SZ_1K;
+        vits_spin_unlock(vits);
+        return 1;
+    case GITS_BASER1 ... GITS_BASERN:
+        /* Nothing to do with this values. Just store and emulate */
+        if ( dabt.size != DABT_DOUBLE_WORD )
+            goto bad_width;
+        if ( (gits_reg % 8) != 0 )
+            goto bad_width;
+        vits_spin_lock(vits);
+        index = (gits_reg - GITS_BASER) / 8;
+        vits->baser[index] = *r;
+        vits_spin_unlock(vits);
+        return 1;
+    case GITS_PIDR7 ... GITS_PIDR0:
+        /* R0 -- write ignored */
+        goto write_ignore;
+   default:
+        dprintk(XENLOG_G_ERR, "vITS:d%dv%d unhandled write r%d offset %#08x\n",
+                v->domain->domain_id, v->vcpu_id, dabt.reg, gits_reg);
+        return 0;
+    }
+
+bad_width:
+    dprintk(XENLOG_G_ERR, "vITS:d%dv%d bad write width %d r%d offset %#08x\n",
+            v->domain->domain_id, v->vcpu_id, dabt.size, dabt.reg, gits_reg);
+    domain_crash_synchronous();
+    return 0;
+
+write_ignore:
+    if ( dabt.size != DABT_WORD ) goto bad_width;
+    *r = 0;
+    return 1;
+}
+
+
+static const struct mmio_handler_ops vgic_gits_mmio_handler = {
+    .read_handler  = vgic_v3_gits_mmio_read,
+    .write_handler = vgic_v3_gits_mmio_write,
+};
+
 /*
  * Local variables:
  * mode: C
xen/include/asm-arm/gic-its.h +14 -0
index 8f898a6..3271477 100644
@@ -38,6 +38,8 @@ struct its_collection {
 struct vgic_its
 {
    spinlock_t lock;
+   /* Emulation of BASER */
+   paddr_t baser[8];
    /* Command queue base */
    paddr_t cmd_base;
    /* Command queue write pointer */
@@ -48,8 +50,20 @@ struct vgic_its
    paddr_t cmd_read;
    /* Command queue size */
    unsigned long cmd_qsize;
+   /* ITS mmio physical base */
+   paddr_t phys_base;
+   /* ITS mmio physical size */
+   unsigned long phys_size;
    /* ITS physical node */
    struct its_node *its;
+   /* GICR ctrl register */
+   uint32_t ctrl;
+   /* LPI propbase */
+   paddr_t propbase;
+   /* percpu pendbase */
+   paddr_t pendbase[MAX_VIRT_CPUS];
+   /* Virtual LPI property table */
+   void * prop_page;
    /* vITT device table ipa */
    paddr_t dt_ipa;
    /* vITT device table size */
Signature · 1 line
1.7.9.5
#61 lore raw

Re: [RFC PATCH v3 11/18] xen/arm: ITS: Add GITS registers emulation

Hi Vijay,

Quoted 4 lines · On 22/06/2015 14:01, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

Emulate GITS* registers and handle LPI configuration table update trap.

The LPI configuration table as nothing to do with the GITS registers. It's related to the GICR. Why didn't you implement it on the next patch?

Quoted 12 lines
---
  xen/arch/arm/vgic-v3-its.c    |  516 +++++++++++++++++++++++++++++++++++++++++
  xen/include/asm-arm/gic-its.h |   14 ++
  2 files changed, 530 insertions(+)
xen/arch/arm/vgic-v3-its.c +0 -0
index 0671434..fa9dccc 100644

xen/arch/arm/vgic-v3-its.c static void dump_cmd(its_cmd_block *cmd) · line 66

Quoted 4 lines
xen/arch/arm/vgic-v3-its.c +1 -0
@@ -63,6 +63,46 @@ static void dump_cmd(its_cmd_block *cmd)
  }
  #endif
 
+void vgic_its_disable_lpis(struct vcpu *v, uint32_t vlpi)

Any function non-exported should be static. Also you are disabling only one lpi, so I would rename the function vgic_its_disable_lpi.

xen/arch/arm/vgic-v3-its.c static void dump_cmd(its_cmd_block *cmd) · line 79

Quoted 13 lines
xen/arch/arm/vgic-v3-its.c +13 -0
+{
+    struct pending_irq *p;
+    unsigned long flags;
+
+    p = irq_to_pending(v, vlpi);
+    clear_bit(GIC_IRQ_GUEST_ENABLED, &p->status);
+    gic_remove_from_queues(v, vlpi);
+    if ( p->desc != NULL )
+    {
+        spin_lock_irqsave(&p->desc->lock, flags);
+        p->desc->handler->disable(p->desc);
+        spin_unlock_irqrestore(&p->desc->lock, flags);
+    }

It's not possible to associate a unique pLPI to a vLPI. This function should only update the pending_state.

xen/arch/arm/vgic-v3-its.c static void dump_cmd(its_cmd_block *cmd) · line 82

Quoted 3 lines
xen/arch/arm/vgic-v3-its.c +3 -0
+}
+
+void vgic_its_enable_lpis(struct vcpu *v, uint32_t vlpi, uint8_t priority)

Ditto for static & name.

xen/arch/arm/vgic-v3-its.c static void dump_cmd(its_cmd_block *cmd) · line 89

Quoted 7 lines
xen/arch/arm/vgic-v3-its.c +7 -0
+{
+    struct pending_irq *p;
+    unsigned long flags;
+
+    /* Get plpi for the given vlpi */
+    p = irq_to_pending(v, vlpi);
+    p->priority = priority;

Why? There is already a callback to get the IRQ priority (see get_irq_priority). It will retrieve the correct priority when the IRQ is injected (and not when it's has been enabled).

In anycase, this is not safe to write in p->pending like that.

xen/arch/arm/vgic-v3-its.c static void dump_cmd(its_cmd_block *cmd) · line 92

Quoted 3 lines
xen/arch/arm/vgic-v3-its.c +3 -0
+    set_bit(GIC_IRQ_GUEST_ENABLED, &p->status);
+
+    spin_lock_irqsave(&v->arch.vgic.lock, flags);

The vIRQ is protected by the CPU where it's routed not the one which enabled. You have to find the right vCPU.

xen/arch/arm/vgic-v3-its.c static void dump_cmd(its_cmd_block *cmd) · line 104

Quoted 12 lines
xen/arch/arm/vgic-v3-its.c +12 -0
+
+    if ( !list_empty(&p->inflight) &&
+         !test_bit(GIC_IRQ_GUEST_VISIBLE, &p->status) )
+        gic_raise_guest_irq(v, irq_to_virq(p->desc), p->priority);
+
+    spin_unlock_irqrestore(&v->arch.vgic.lock, flags);
+    if ( p->desc != NULL )
+    {
+        spin_lock_irqsave(&p->desc->lock, flags);
+        p->desc->handler->enable(p->desc);
+        spin_unlock_irqrestore(&p->desc->lock, flags);
+    }

Ditto for the pLPI to vLPI mapping.

xen/arch/arm/vgic-v3-its.c static void dump_cmd(its_cmd_block *cmd) · line 105

Quoted 1 line
xen/arch/arm/vgic-v3-its.c +1 -0
+}

Missing newline

xen/arch/arm/vgic-v3-its.c err: · line 692

Quoted 8 lines
xen/arch/arm/vgic-v3-its.c +1 -0
  /* ITS device table helper functions */
  int vits_vdevice_entry(struct domain *d, uint32_t dev_id,
                         struct vdevice_table *entry, int set)
@@ -649,6 +689,482 @@ err:
      return 0;
  }
 
+static int vgic_v3_gits_lpi_mmio_read(struct vcpu *v, mmio_info_t *info)

The LPI configuration table is not ITS specific but GICv3. Although, I'm fine if you let this code here for the time being.

xen/arch/arm/vgic-v3-its.c err: · line 701

Quoted 9 lines
xen/arch/arm/vgic-v3-its.c +9 -0
+{
+    uint32_t offset;
+    struct hsr_dabt dabt = info->dabt;
+    struct cpu_user_regs *regs = guest_cpu_user_regs();
+    register_t *r = select_user_reg(regs, dabt.reg);
+    uint8_t cfg;
+
+    offset = info->gpa -
+             (v->domain->arch.vits->propbase & 0xfffffffff000UL);

You are using 0xffff... in multiple place. Please add a define and use it.

xen/arch/arm/vgic-v3-its.c err: · line 703

Quoted 2 lines
xen/arch/arm/vgic-v3-its.c +2 -0
+
+    if ( offset < SZ_64K )

AFAICT, the LPI configuration table can be smaller and higher than 64K. Although, this check is not necessary because you have registered the handler on a valid MMIO range.

xen/arch/arm/vgic-v3-its.c err: · line 706

Quoted 3 lines
xen/arch/arm/vgic-v3-its.c +3 -0
+    {
+        DPRINTK("vITS:d%dv%d LPI Table read offset 0x%x\n",
+                v->domain->domain_id, v->vcpu_id, offset);

%pv rather than d%dv%d

xen/arch/arm/vgic-v3-its.c err: · line 707

Quoted 1 line
xen/arch/arm/vgic-v3-its.c +1 -0
+        cfg = readb_relaxed(v->domain->arch.vits->prop_page + offset);

Why do you use readb? Those helpers have been created to read MMIO not Xen memory.

Although what about other access? a 64/32/16 bits access are valid and will return the wrong value.

Quoted 19 lines
xen/arch/arm/vgic-v3-its.c +19 -0
+        *r = cfg;
+        return 1;
+    }
+    else
+        dprintk(XENLOG_G_ERR, "vITS:d%dv%d LPI Table read with wrong offset 0x%x\n",
+                v->domain->domain_id, v->vcpu_id, offset);
+
+
+    return 0;
+}
+
+static int vgic_v3_gits_lpi_mmio_write(struct vcpu *v, mmio_info_t *info)
+{
+    uint32_t offset;
+    uint32_t vid;
+    uint8_t cfg;
+    bool_t enable;
+    struct hsr_dabt dabt = info->dabt;
+    struct cpu_user_regs *regs = guest_cpu_user_regs();

xen/arch/arm/vgic-v3-its.c err: · line 736

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+    register_t *r = select_user_reg(regs, dabt.reg);
+
+    offset = info->gpa -
+             (v->domain->arch.vits->propbase & 0xfffffffff000UL);
+
+    vid = offset + NR_GIC_LPI;
+    if ( offset < SZ_64K )
+    {
+        DPRINTK("vITS:d%dv%d LPI Table write offset 0x%x\n",
+                v->domain->domain_id, v->vcpu_id, offset);

%pv rather than d%dv%d

xen/arch/arm/vgic-v3-its.c err: · line 738

Quoted 2 lines
xen/arch/arm/vgic-v3-its.c +2 -0
+        cfg = readb_relaxed(v->domain->arch.vits->prop_page + offset);
+        enable = (cfg & *r) & 0x1;

Please use a define rather than 0x1. It would have been more clear that we are checking the enable bit.

xen/arch/arm/vgic-v3-its.c err: · line 741

Quoted 3 lines
xen/arch/arm/vgic-v3-its.c +3 -0
+
+        if ( !enable )
+             vgic_its_enable_lpis(v, vid,  (*r & 0xfc));

Please a define for the priority mask.

xen/arch/arm/vgic-v3-its.c err: · line 747

Quoted 6 lines
xen/arch/arm/vgic-v3-its.c +6 -0
+        else
+             vgic_its_disable_lpis(v, vid);
+
+        /* Update virtual prop page */
+        writeb_relaxed((*r & 0xff),
+                        v->domain->arch.vits->prop_page + offset);

Same remark as readb_relaxed. You also need to handle all the other possible write access.

Finally, this function is racy. Nothing prevent 2 vCPUs to write in the LPI configuration table. This may result to incoherency between the state of the pending_irq and the Configuration Table.

You have to provide at least a locking for the Configuration Table.

Quoted 6 lines
xen/arch/arm/vgic-v3-its.c +6 -0
+
+        return 1;
+    }
+    else
+        dprintk(XENLOG_G_ERR, "vITS:d%dv%d LPI Table invalid write @ 0x%x\n",
+                v->domain->domain_id, v->vcpu_id, offset);

xen/arch/arm/vgic-v3-its.c err: · line 763

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+
+    return 0;
+}
+
+static const struct mmio_handler_ops vgic_gits_lpi_mmio_handler = {
+    .read_handler  = vgic_v3_gits_lpi_mmio_read,
+    .write_handler = vgic_v3_gits_lpi_mmio_write,
+};
+
+int vgic_its_unmap_lpi_prop(struct vcpu *v)

Please define the prototype of this function in the header within the same file if you plan to export. If not, it should be static.

xen/arch/arm/vgic-v3-its.c err: · line 770

Quoted 7 lines
xen/arch/arm/vgic-v3-its.c +7 -0
+{
+    paddr_t maddr;
+    uint32_t lpi_size;
+    int i;
+
+    maddr = v->domain->arch.vits->propbase & 0xfffffffff000UL;
+    lpi_size = 1UL << ((v->domain->arch.vits->propbase & 0x1f) + 1);

Please use a define for the 0x1f.

xen/arch/arm/vgic-v3-its.c err: · line 775

Quoted 5 lines
xen/arch/arm/vgic-v3-its.c +5 -0
+
+    DPRINTK("vITS:d%dv%d Unmap guest LPI conf table maddr 0x%lx lpi_size 0x%x\n",
+             v->domain->domain_id, v->vcpu_id, maddr, lpi_size);
+
+    if ( lpi_size < SZ_64K )

Why this restriction? The IDbits can encode up to 32 bits interrupt identifier.

You have to check this value against GICD_TYPER.IDbits.

xen/arch/arm/vgic-v3-its.c err: · line 782

Quoted 7 lines
xen/arch/arm/vgic-v3-its.c +7 -0
+    {
+        dprintk(XENLOG_G_ERR, "vITS:d%dv%d LPI Prop page < 64K\n",
+                v->domain->domain_id, v->vcpu_id);
+        return 0;
+    }
+
+    /* XXX: As per 4.8.9 each re-distributor shares a common LPI configuration table

Coding style:
/*
* ...
*/

Also XXX means TODO.

4.8.9 of what? Please provide a section for the public doc (i.e IHI0069A...) and not private.

xen/arch/arm/vgic-v3-its.c err: · line 787

Quoted 5 lines
xen/arch/arm/vgic-v3-its.c +5 -0
+     * So one set of mmio handlers to manage configuration table is enough
+     */
+    for ( i = 0; i < lpi_size / PAGE_SIZE; i++ )
+        guest_physmap_remove_page(v->domain, paddr_to_pfn(maddr),
+                                gmfn_to_mfn(v->domain, paddr_to_pfn(maddr)), 0);

No validation at all on the address pass for the guest? gmfn_to_mfn can return an invalid MFN and I'm not sure what would happen if the guest is trying to pass other things than RAM.

xen/arch/arm/vgic-v3-its.c err: · line 795

Quoted 8 lines
xen/arch/arm/vgic-v3-its.c +8 -0
+
+    /* Register mmio handlers for this region */
+    register_mmio_handler(v->domain, &vgic_gits_lpi_mmio_handler,
+                          maddr, lpi_size);
+
+    /* Allocate Virtual LPI Property table */
+    v->domain->arch.vits->prop_page =
+        alloc_xenheap_pages(get_order_from_bytes(lpi_size), 0);

I wasn't able to find a place where you free the pages allocated... Please ensure that any allocated memory is freed when the guest is destroyed.

Rather than allocating the page from Xen memory, I'm wondering if we can re-use the page unmapped.

If not, I would prefer to see this allocation at domain creation time rather than when the domain is setting PROPBASER. The main reason is with the current implementation, the guest could abuse of PROPBASER and exhaust the Xen memory. It would require to do some management here.

Also, what if the guest decide to change it's property table? This is valid and I think we will hit with the suspend/resume case. I'm fine if you don't want to handle it now, but please make it obvious that you don't handle it.

xen/arch/arm/vgic-v3-its.c err: · line 803

Quoted 8 lines
xen/arch/arm/vgic-v3-its.c +8 -0
+    if ( !v->domain->arch.vits->prop_page )
+    {
+        dprintk(XENLOG_G_ERR, "vITS:d%dv%d Failed to allocate LPI Prop page\n",
+                v->domain->domain_id, v->vcpu_id);
+        return 0;
+    }
+
+    memset(v->domain->arch.vits->prop_page, 0xa2, lpi_size);

Where does this value come from? It's up to the guest to decide what will be the priority of vLPI. Also, have you a doc reference to say that we should ignore any value in the initial set of pages?

Quoted 14 lines
xen/arch/arm/vgic-v3-its.c +14 -0
+
+    return 1;
+}
+
+static inline void vits_spin_lock(struct vgic_its *vits)
+{
+    spin_lock(&vits->lock);
+}
+
+static inline void vits_spin_unlock(struct vgic_its *vits)
+{
+    spin_unlock(&vits->lock);
+}
+

xen/arch/arm/vgic-v3-its.c err: · line 827

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+static int vgic_v3_gits_mmio_read(struct vcpu *v, mmio_info_t *info)
+{
+    struct vgic_its *vits;
+    struct hsr_dabt dabt = info->dabt;
+    struct cpu_user_regs *regs = guest_cpu_user_regs();
+    register_t *r = select_user_reg(regs, dabt.reg);
+    uint64_t val = 0;
+    uint32_t index, gits_reg;
+
+    vits = v->domain->arch.vits;

You can directly do struct vgic_its *vits = v->domain->arch.vits

xen/arch/arm/vgic-v3-its.c err: · line 831

Quoted 4 lines
xen/arch/arm/vgic-v3-its.c +4 -0
+
+    gits_reg = info->gpa - vits->phys_base;
+
+    if ( gits_reg >= SZ_64K )

This can never happen, you always register a valid 64K range.

xen/arch/arm/vgic-v3-its.c err: · line 844

Quoted 13 lines
xen/arch/arm/vgic-v3-its.c +13 -0
+    {
+        gdprintk(XENLOG_G_WARNING,
+                 "vITS:d%dv%d unknown gpa read address %"PRIpaddr"\n",
+                 v->domain->domain_id, v->vcpu_id, info->gpa);
+        return 0;
+    }
+
+    switch ( gits_reg )
+    {
+    case GITS_CTLR:
+        if ( dabt.size != DABT_WORD )
+            goto bad_width;
+        *r = 0;

Missing implementation of GITS_CTLR.

xen/arch/arm/vgic-v3-its.c err: · line 849

Quoted 5 lines
xen/arch/arm/vgic-v3-its.c +5 -0
+        return 1;
+    case GITS_IIDR:
+        if ( dabt.size != DABT_WORD )
+            goto bad_width;
+        *r = 0;

Missing implementaiton of GITS_IIDR

xen/arch/arm/vgic-v3-its.c err: · line 852

Quoted 3 lines
xen/arch/arm/vgic-v3-its.c +3 -0
+        return 1;
+    case GITS_TYPER:
+        vits_spin_lock(vits);

Why the lock?

Please add a comment explaining the configuration of this field. It would avoid to scratch his head try to understand why PTA is not mentioned...

xen/arch/arm/vgic-v3-its.c err: · line 853

Quoted 1 line
xen/arch/arm/vgic-v3-its.c +1 -0
+        val = (((v->domain->max_vcpus + 1) << GITS_TYPER_HCC_SHIFT ) |

I would prefer to see a macro to compute the number of collection.

Although, HCC is only able to encore 16-bit of collection ID (i.e 256 collection => 255 VCPUs). Please make a note somewhere and add a BUILD_ON_BUG on anything else to ensure that it will never happen.

HCC is only able to encode 16-bit collection ID (i.e 255 collection).

xen/arch/arm/vgic-v3-its.c err: · line 854

Quoted 1 line
xen/arch/arm/vgic-v3-its.c +1 -0
+                 VITS_GITS_DEV_BITS | VITS_GITS_ID_BITS              |

Please introduce new fields in vits to store the number of deviceID bits and eventID bits. Don't harcode them.

xen/arch/arm/vgic-v3-its.c err: · line 855

Quoted 1 line
xen/arch/arm/vgic-v3-its.c +1 -0
+                 VITS_GITS_ITT_SIZE | VITS_GITS_DISTRIBUTED          |

The bit 3 is marked as implementation defined. Why do you set and name it DISTRIBUTED?

xen/arch/arm/vgic-v3-its.c err: · line 868

Quoted 13 lines
xen/arch/arm/vgic-v3-its.c +13 -0
+                 VITS_GITS_PLPIS);
+        if ( dabt.size == DABT_DOUBLE_WORD )
+            *r = val;
+        else if ( dabt.size == DABT_WORD )
+            *r = (u32)val;
+        else
+        {
+            vits_spin_unlock(vits);
+            goto bad_width;
+        }
+        vits_spin_unlock(vits);
+        return 1;
+    case GITS_TYPER + 4:

I don't like the idea to duplicate the code for GITS_TYPER just for reading the top word. Isn't possible to merge the 2 switch case?

Quoted 5 lines
xen/arch/arm/vgic-v3-its.c +5 -0
+        if (dabt.size != DABT_WORD ) goto bad_width;
+        vits_spin_lock(vits);
+        val = (((v->domain->max_vcpus + 1) << GITS_TYPER_HCC_SHIFT ) |
+                 VITS_GITS_DEV_BITS | VITS_GITS_ID_BITS              |
+                 VITS_GITS_ITT_SIZE | VITS_GITS_DISTRIBUTED          |

xen/arch/arm/vgic-v3-its.c err: · line 883

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+                 VITS_GITS_PLPIS);
+        *r = (u32)(val >> 32);
+        vits_spin_unlock(vits);
+        return 1;
+    case 0x0010 ... 0x007c:
+    case 0xc000 ... 0xffcc:
+        /* Implementation defined -- read ignored */
+        dprintk(XENLOG_ERR,
+                "vITS:d%dv%d read unknown 0x000c - 0x007c r%d offset %#08x\n",
+                v->domain->domain_id, v->vcpu_id, dabt.reg, gits_reg);

Please don't use XENLOG_ERR in guest code. Also, this printk is not useful and has been dropped in other emulation.

xen/arch/arm/vgic-v3-its.c err: · line 886

Quoted 3 lines
xen/arch/arm/vgic-v3-its.c +3 -0
+        goto read_as_zero;
+    case GITS_CBASER:
+        /* XXX: Only read support 32/64-bit access */

XXX means TODO which is not the case here.

xen/arch/arm/vgic-v3-its.c err: · line 889

Quoted 3 lines
xen/arch/arm/vgic-v3-its.c +3 -0
+        vits_spin_lock(vits);
+        if ( dabt.size == DABT_DOUBLE_WORD )
+            *r = vits->cmd_base && 0xc7ffffffffffffffUL;

Why the && and what does mean this constant?

xen/arch/arm/vgic-v3-its.c err: · line 897

Quoted 8 lines
xen/arch/arm/vgic-v3-its.c +8 -0
+        else if ( dabt.size == DABT_WORD )
+            *r = (u32)vits->cmd_base;
+        else
+        {
+            vits_spin_unlock(vits);
+            goto bad_width;
+        }
+        vits_spin_unlock(vits);

The lock section could have been smaller and avoid to duplicat vits_spin_unlock by first reading the value then setting the register.

xen/arch/arm/vgic-v3-its.c err: · line 899

Quoted 2 lines
xen/arch/arm/vgic-v3-its.c +2 -0
+        return 1;
+    case GITS_CBASER + 4:

Same remark as GITS_TYPER for the support for reading word.

xen/arch/arm/vgic-v3-its.c err: · line 900

Quoted 1 line
xen/arch/arm/vgic-v3-its.c +1 -0
+        if (dabt.size != DABT_WORD )

if ( ... )

xen/arch/arm/vgic-v3-its.c err: · line 906

Quoted 6 lines
xen/arch/arm/vgic-v3-its.c +6 -0
+            goto bad_width;
+        vits_spin_lock(vits);
+        *r = (u32)(vits->cmd_base >> 32);
+        vits_spin_unlock(vits);
+        return 1;
+    case GITS_CWRITER:

See all my comment on CBASER.

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+        /* XXX: Only read support 32/64-bit access */
+        vits_spin_lock(vits);
+        if ( dabt.size == DABT_DOUBLE_WORD )
+            *r = vits->cmd_write;
+        else if ( dabt.size == DABT_WORD )
+            *r = (u32)vits->cmd_write;
+        else
+        {
+            vits_spin_unlock(vits);
+            goto bad_width;

xen/arch/arm/vgic-v3-its.c err: · line 926

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+        }
+        vits_spin_unlock(vits);
+        return 1;
+    case GITS_CWRITER + 4:
+        if ( dabt.size != DABT_WORD )
+            goto bad_width;
+        vits_spin_lock(vits);
+        *r = (u32)(vits->cmd_write >> 32);
+        vits_spin_unlock(vits);
+        return 1;

ditto for word-read

Quoted 11 lines
xen/arch/arm/vgic-v3-its.c +11 -0
+    case GITS_CREADR:
+        /* XXX: Only read support 32/64-bit access */
+        vits_spin_lock(vits);
+        if ( dabt.size == DABT_DOUBLE_WORD )
+            *r = vits->cmd_read;
+        else if ( dabt.size == DABT_WORD )
+            *r = (u32)vits->cmd_read;
+        else
+        {
+            vits_spin_unlock(vits);
+            goto bad_width;

xen/arch/arm/vgic-v3-its.c err: · line 947

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+        }
+        vits_spin_unlock(vits);
+        return 1;
+    case GITS_CREADR + 4:
+        if ( dabt.size != DABT_WORD )
+            goto bad_width;
+        vits_spin_lock(vits);
+        *r = (u32)(vits->cmd_read >> 32);
+        vits_spin_unlock(vits);
+        return 1;

ditto

xen/arch/arm/vgic-v3-its.c err: · line 954

Quoted 7 lines
xen/arch/arm/vgic-v3-its.c +7 -0
+    case 0x0098 ... 0x009c:
+    case 0x00a0 ... 0x00fc:
+    case 0x0140 ... 0xbffc:
+        /* Reserved -- read ignored */
+        dprintk(XENLOG_ERR,
+                "vITS:d%dv%d read unknown 0x0098-9c or 0x00a0-fc r%d offset %#08x\n",
+                v->domain->domain_id, v->vcpu_id, dabt.reg, gits_reg);

No need to the message.

xen/arch/arm/vgic-v3-its.c err: · line 957

Quoted 3 lines
xen/arch/arm/vgic-v3-its.c +3 -0
+        goto read_as_zero;
+    case GITS_BASER ... GITS_BASERN:
+        /* Supports only 64-bit access */

The XXX would have been useful here ;)

xen/arch/arm/vgic-v3-its.c err: · line 960

Quoted 3 lines
xen/arch/arm/vgic-v3-its.c +3 -0
+        if ( dabt.size != DABT_DOUBLE_WORD )
+            goto bad_width;
+        if ( (gits_reg % 8) != 0 )

What is used for?

xen/arch/arm/vgic-v3-its.c err: · line 965

Quoted 5 lines
xen/arch/arm/vgic-v3-its.c +5 -0
+            goto bad_width;
+        vits_spin_lock(vits);
+        index = (gits_reg - GITS_BASER) / 8;
+        *r = vits->baser[index];
+        vits_spin_unlock(vits);

Based on the spec (8.19.1 in IHI0069A), any unimplemented register should be RES0.

As we only support BASER0, I would only implemented it with a check all the other would go to the read_as_zero.

Quoted 18 lines
xen/arch/arm/vgic-v3-its.c +18 -0
+        return 1;
+    case GITS_PIDR0:
+        if ( dabt.size != DABT_WORD )
+            goto bad_width;
+        *r = GITS_PIDR0_VAL;
+        return 1;
+    case GITS_PIDR1:
+        if ( dabt.size != DABT_WORD )
+            goto bad_width;
+        *r = GITS_PIDR1_VAL;
+        return 1;
+    case GITS_PIDR2:
+        if ( dabt.size != DABT_WORD )
+            goto bad_width;
+        *r = GITS_PIDR2_VAL;
+        return 1;
+    case GITS_PIDR3:
+        if ( dabt.size != DABT_WORD )

xen/arch/arm/vgic-v3-its.c err: · line 993

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+            goto bad_width;
+        *r = GITS_PIDR3_VAL;
+        return 1;
+    case GITS_PIDR4:
+        if ( dabt.size != DABT_WORD )
+            goto bad_width;
+        *r = GITS_PIDR4_VAL;
+        return 1;
+    case GITS_PIDR5 ... GITS_PIDR7:
+        goto read_as_zero;

Please check that we access is done via a word-access by introducing a new label read_as_zero_32 (for instance see the vgic v2 emulation).

xen/arch/arm/vgic-v3-its.c err: · line 1002

Quoted 9 lines
xen/arch/arm/vgic-v3-its.c +9 -0
+   default:
+        dprintk(XENLOG_G_ERR, "vITS:d%dv%d unhandled read r%d offset %#08x\n",
+                v->domain->domain_id, v->vcpu_id, dabt.reg, gits_reg);
+        return 0;
+    }
+
+bad_width:
+    dprintk(XENLOG_G_ERR, "vITS:d%dv%d bad read width %d r%d offset %#08x\n",
+           v->domain->domain_id, v->vcpu_id, dabt.size, dabt.reg, gits_reg);

printk(XENLOG_G_ERR "%pv: ....", v,...)

xen/arch/arm/vgic-v3-its.c err: · line 1007

Quoted 5 lines
xen/arch/arm/vgic-v3-its.c +5 -0
+    domain_crash_synchronous();
+    return 0;
+
+read_as_zero:
+    if ( dabt.size != DABT_WORD )

How do you know that all RAZ access 32-bit access? See implementation defined registers for instance.

I would prefer to introduce multiple label:

read_as_zero_32: /* RAZ 32-bit */

    if ( dabt.size != DABT_WORD ) goto bad_width;

read_as_zero: /* Not check necessary */

    *r = 0;

And use the correctly label for goto in the emulation. So the code would be self-documented too.

xen/arch/arm/vgic-v3-its.c err: · line 1015

Quoted 8 lines
xen/arch/arm/vgic-v3-its.c +8 -0
+       goto bad_width;
+    *r = 0;
+    return 1;
+}
+
+#define GITS_BASER_MASK  (~((0x7UL << GITS_BASER_TYPE_SHIFT) | \
+                         (0xffUL << GITS_BASER_ENTRY_SIZE_SHIFT) | \
+                         (0x3UL << GITS_BASER_SHAREABILITY_SHIFT)))

Please add a comment to explain what the mask is used for.

Quoted 12 lines
xen/arch/arm/vgic-v3-its.c +12 -0
+
+static int vgic_v3_gits_mmio_write(struct vcpu *v, mmio_info_t *info)
+{
+    struct vgic_its *vits;
+    struct hsr_dabt dabt = info->dabt;
+    struct cpu_user_regs *regs = guest_cpu_user_regs();
+    register_t *r = select_user_reg(regs, dabt.reg);
+    int ret;
+    uint32_t index, gits_reg, sz, psz;
+    uint64_t val;
+
+    vits = v->domain->arch.vits;

xen/arch/arm/vgic-v3-its.c err: · line 1037

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+
+    gits_reg = info->gpa - vits->phys_base;
+
+    if ( gits_reg >= SZ_64K )
+    {
+        gdprintk(XENLOG_G_WARNING,
+                 "vITS:d%dv%d unknown gpa write address %"PRIpaddr"\n",
+                 v->domain->domain_id, v->vcpu_id, info->gpa);
+        return 0;
+    }

This check is not necessary.

xen/arch/arm/vgic-v3-its.c err: · line 1045

Quoted 8 lines
xen/arch/arm/vgic-v3-its.c +8 -0
+    switch ( gits_reg )
+    {
+    case GITS_CTLR:
+        if ( dabt.size != DABT_WORD )
+            goto bad_width;
+        vits_spin_lock(vits);
+        vits->ctrl = *r;
+        vits_spin_unlock(vits);

Only bit[0] (Enabled) is writable.

xen/arch/arm/vgic-v3-its.c err: · line 1053

Quoted 8 lines
xen/arch/arm/vgic-v3-its.c +8 -0
+        return 1;
+    case GITS_IIDR:
+        /* R0 -- write ignored */
+        goto write_ignore;
+    case GITS_TYPER:
+    case GITS_TYPER + 4:
+        /* R0 -- write ignored */
+        goto write_ignore;

Please explicitly check the access size. That would avoid to crash the guest when TYPER is write with a 64-bit access.

xen/arch/arm/vgic-v3-its.c err: · line 1059

Quoted 6 lines
xen/arch/arm/vgic-v3-its.c +6 -0
+    case 0x0010 ... 0x007c:
+    case 0xc000 ... 0xffcc:
+        /* Implementation defined -- write ignored */
+        dprintk(XENLOG_G_ERR,
+                "vITS:d%dv%d write to unknown 0x000c - 0x007c r%d offset %#08x\n",
+                v->domain->domain_id, v->vcpu_id, dabt.reg, gits_reg);

Please drop the dprintk.

xen/arch/arm/vgic-v3-its.c err: · line 1061

Quoted 2 lines
xen/arch/arm/vgic-v3-its.c +2 -0
+        goto write_ignore;
+    case GITS_CBASER:

GITS_CBASER is read-only when GITS_CTLR.Enable is Zero or GITS_CTLR.Quiescent is zero.

xen/arch/arm/vgic-v3-its.c err: · line 1062

Quoted 1 line
xen/arch/arm/vgic-v3-its.c +1 -0
+        if ( dabt.size != DABT_DOUBLE_WORD )

Missing a TODO for 32bit access support.

xen/arch/arm/vgic-v3-its.c err: · line 1066

Quoted 4 lines
xen/arch/arm/vgic-v3-its.c +4 -0
+            goto bad_width;
+        vits_spin_lock(vits);
+        vits->cmd_base = *r;
+        vits->cmd_qsize  =  SZ_4K * ((*r & 0xff) + 1);

Please use a define for the mask.
Also I would use cmd_qsize to know if the valid is set or not. I.e cmd_qsize = 0 => command queue not valid.

You forgot to update GITS_CREADR (i.e setting to 0) when GITS_CBASER is successfully written (8.19.2 in IHI0069A).

xen/arch/arm/vgic-v3-its.c err: · line 1075

Quoted 9 lines
xen/arch/arm/vgic-v3-its.c +9 -0
+        vits_spin_unlock(vits);
+        return 1;
+    case GITS_CBASER + 4:
+         /* XXX: Does not support word write */
+        goto bad_width;
+    case GITS_CWRITER:
+        vits_spin_lock(vits);
+        if ( dabt.size == DABT_DOUBLE_WORD )
+            vits->cmd_write = *r;

Only Bits[19:5] are writable.

xen/arch/arm/vgic-v3-its.c err: · line 1079

Quoted 4 lines
xen/arch/arm/vgic-v3-its.c +4 -0
+        else if ( dabt.size == DABT_WORD)
+        {
+            val = vits->cmd_write & 0xffffffff00000000UL;
+            val = (*r) | val;

Only Bits[19:5] are writable.

xen/arch/arm/vgic-v3-its.c err: · line 1086

Quoted 7 lines
xen/arch/arm/vgic-v3-its.c +7 -0
+            vits->cmd_write =  val;
+        }
+        else
+        {
+            vits_spin_unlock(vits);
+            goto bad_width;
+        }

This could be simplified to avoid 2 vits_spin_unlock.

Also, no validation of the value written by the guest? Given your implementation of the command processing, any invalid value will end up to an infinite loop in the hypervisor. Whoops :).

xen/arch/arm/vgic-v3-its.c err: · line 1090

Quoted 4 lines
xen/arch/arm/vgic-v3-its.c +4 -0
+        ret = vgic_its_process_cmd(v, vits);
+        vits_spin_unlock(vits);
+        return ret;
+    case GITS_CWRITER + 4:

Only the Bits[19:5] of GITS_CWRITER are writable. So the emulation is pointless here.

Quoted 9 lines
xen/arch/arm/vgic-v3-its.c +9 -0
+        if (dabt.size != DABT_WORD )
+            goto bad_width;
+        vits_spin_lock(vits);
+        val = vits->cmd_write & 0xffffffffUL;
+        val = ((*r & 0xffffffffUL) << 32) | val;
+        vits->cmd_write =  val;
+        ret = vgic_its_process_cmd(v, vits);
+        vits_spin_unlock(vits);
+        return ret;

xen/arch/arm/vgic-v3-its.c err: · line 1109

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+    case GITS_CREADR:
+        /* R0 -- write ignored */
+        goto write_ignore;
+    case 0x0098 ... 0x009c:
+    case 0x00a0 ... 0x00fc:
+    case 0x0140 ... 0xbffc:
+        /* Reserved -- write ignored */
+        dprintk(XENLOG_G_ERR,
+                "vITS:d%dv%d write to unknown 0x98-9c or 0xa0-fc r%d offset %#08x\n",
+                v->domain->domain_id, v->vcpu_id, dabt.reg, gits_reg);

Please drop the printk

xen/arch/arm/vgic-v3-its.c err: · line 1112

Quoted 3 lines
xen/arch/arm/vgic-v3-its.c +3 -0
+        goto write_ignore;
+    case GITS_BASER0:
+        if ( dabt.size != DABT_DOUBLE_WORD )

/* XXX: Support 32 bit access */

xen/arch/arm/vgic-v3-its.c err: · line 1115

Quoted 3 lines
xen/arch/arm/vgic-v3-its.c +3 -0
+            goto bad_width;
+        vits_spin_lock(vits);
+        vits->baser[0] = vits->baser[0] | (GITS_BASER_MASK & *r);

I would prefer to see *r & GITS_BASER_MASK which is more logical to read.

Although this doesn't do what you want. This will not erase bit set to 1 in baser.

Quoted 6 lines
xen/arch/arm/vgic-v3-its.c +6 -0
+        vits->dt_ipa = vits->baser[0] & 0xfffffffff000UL;
+        psz = (vits->baser[0] >> GITS_BASER_PAGE_SIZE_SHIFT) &
+               GITS_BASER_PAGE_SIZE_MASK_VAL;
+        if ( psz == GITS_BASER_PAGE_SIZE_4K_VAL )
+            sz = 4;
+        else if ( psz == GITS_BASER_PAGE_SIZE_16K_VAL )

xen/arch/arm/vgic-v3-its.c err: · line 1131

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+            sz = 16;
+        else
+            sz = 64;
+
+        vits->dt_size = (vits->baser[0] & GITS_BASER_PAGES_MASK_VAL)
+                        * sz * SZ_1K;
+        vits_spin_unlock(vits);
+        return 1;
+    case GITS_BASER1 ... GITS_BASERN:
+        /* Nothing to do with this values. Just store and emulate */

From the spec these register should be RES0. There is nothing to do here.

Quoted 5 lines
xen/arch/arm/vgic-v3-its.c +5 -0
+        if ( dabt.size != DABT_DOUBLE_WORD )
+            goto bad_width;
+        if ( (gits_reg % 8) != 0 )
+            goto bad_width;
+        vits_spin_lock(vits);

xen/arch/arm/vgic-v3-its.c err: · line 1146

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+        index = (gits_reg - GITS_BASER) / 8;
+        vits->baser[index] = *r;
+        vits_spin_unlock(vits);
+        return 1;
+    case GITS_PIDR7 ... GITS_PIDR0:
+        /* R0 -- write ignored */
+        goto write_ignore;
+   default:
+        dprintk(XENLOG_G_ERR, "vITS:d%dv%d unhandled write r%d offset %#08x\n",
+                v->domain->domain_id, v->vcpu_id, dabt.reg, gits_reg);

printk(XENLOG_G_ERR "VITS %pv: .....", v, ....);

xen/arch/arm/vgic-v3-its.c err: · line 1152

Quoted 6 lines
xen/arch/arm/vgic-v3-its.c +6 -0
+        return 0;
+    }
+
+bad_width:
+    dprintk(XENLOG_G_ERR, "vITS:d%dv%d bad write width %d r%d offset %#08x\n",
+            v->domain->domain_id, v->vcpu_id, dabt.size, dabt.reg, gits_reg);

Ditto

xen/arch/arm/vgic-v3-its.c err: · line 1156

Quoted 4 lines
xen/arch/arm/vgic-v3-its.c +4 -0
+    domain_crash_synchronous();
+    return 0;
+
+write_ignore:

Same remark as read_ignore.

xen/arch/arm/vgic-v3-its.c err: · line 1167

Quoted 11 lines
xen/arch/arm/vgic-v3-its.c +11 -0
+    if ( dabt.size != DABT_WORD ) goto bad_width;
+    *r = 0;
+    return 1;
+}
+
+
+static const struct mmio_handler_ops vgic_gits_mmio_handler = {
+    .read_handler  = vgic_v3_gits_mmio_read,
+    .write_handler = vgic_v3_gits_mmio_write,
+};
+

Any patch should be compilable on standalone to avoid breaking bisection. I believe this is not the case here.

As suggested earlier, I would prefer the Makefile change to include this file when everything is done. This would avoid you s//static/ changes...

xen/include/asm-arm/gic-its.h struct its_collection { · line 42

Quoted 13 lines
xen/arch/arm/vgic-v3-its.c +0 -0
  /*
   * Local variables:
   * mode: C
xen/include/asm-arm/gic-its.h +2 -0
index 8f898a6..3271477 100644
@@ -38,6 +38,8 @@ struct its_collection {
  struct vgic_its
  {
     spinlock_t lock;
+   /* Emulation of BASER */
+   paddr_t baser[8];

I don't think this is useful.

xen/include/asm-arm/gic-its.h struct vgic_its · line 56

Quoted 11 lines
xen/include/asm-arm/gic-its.h +4 -0
     /* Command queue base */
     paddr_t cmd_base;
     /* Command queue write pointer */
@@ -48,8 +50,20 @@ struct vgic_its
     paddr_t cmd_read;
     /* Command queue size */
     unsigned long cmd_qsize;
+   /* ITS mmio physical base */
+   paddr_t phys_base;
+   /* ITS mmio physical size */
+   unsigned long phys_size;

The names are misleading, I though it was referring to the hardware address.

I would prefer if you rename into gits_base and gits_size.

xen/include/asm-arm/gic-its.h struct vgic_its · line 66

Quoted 10 lines
xen/include/asm-arm/gic-its.h +8 -0
     /* ITS physical node */
     struct its_node *its;
+   /* GICR ctrl register */
+   uint32_t ctrl;
+   /* LPI propbase */
+   paddr_t propbase;
+   /* percpu pendbase */
+   paddr_t pendbase[MAX_VIRT_CPUS];
+   /* Virtual LPI property table */
+   void * prop_page;

Coding style: void *prop_page

xen/include/asm-arm/gic-its.h struct vgic_its · line 69

Quoted 4 lines
xen/include/asm-arm/gic-its.h +0 -0
     /* vITT device table ipa */
     paddr_t dt_ipa;
     /* vITT device table size */

Regards,

Signature · 1 line
Julien Grall
#62 lore raw

Re: [RFC PATCH v3 11/18] xen/arm: ITS: Add GITS registers emulation

In reply to [email protected]
Quoted 4 lines · On Mon, 2015-06-22 at 17:31 +0530, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

Emulate GITS* registers and handle LPI configuration table update trap.

These need to only be exposed to a guest which has been configured with an ITS. For dom0 that means at a minimum it needs to be based on the capabilities of the underlying hardware.

The same is true of the next patch adding the GICR registers.

For domU it seems there is currently no ITS exposed to them, since there is no toolstack changes here, so the emulation should be configured accordingly.

Quoted 575 lines
---
 xen/arch/arm/vgic-v3-its.c    |  516 +++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h |   14 ++
 2 files changed, 530 insertions(+)
xen/arch/arm/vgic-v3-its.c +516 -0
index 0671434..fa9dccc 100644
@@ -63,6 +63,46 @@ static void dump_cmd(its_cmd_block *cmd)
 }
 #endif
 
+void vgic_its_disable_lpis(struct vcpu *v, uint32_t vlpi)
+{
+    struct pending_irq *p;
+    unsigned long flags;
+
+    p = irq_to_pending(v, vlpi);
+    clear_bit(GIC_IRQ_GUEST_ENABLED, &p->status);
+    gic_remove_from_queues(v, vlpi);
+    if ( p->desc != NULL )
+    {
+        spin_lock_irqsave(&p->desc->lock, flags);
+        p->desc->handler->disable(p->desc);
+        spin_unlock_irqrestore(&p->desc->lock, flags);
+    }
+}
+
+void vgic_its_enable_lpis(struct vcpu *v, uint32_t vlpi, uint8_t priority)
+{
+    struct pending_irq *p;
+    unsigned long flags;
+
+    /* Get plpi for the given vlpi */
+    p = irq_to_pending(v, vlpi);
+    p->priority = priority;
+    set_bit(GIC_IRQ_GUEST_ENABLED, &p->status);
+
+    spin_lock_irqsave(&v->arch.vgic.lock, flags);
+
+    if ( !list_empty(&p->inflight) &&
+         !test_bit(GIC_IRQ_GUEST_VISIBLE, &p->status) )
+        gic_raise_guest_irq(v, irq_to_virq(p->desc), p->priority);
+
+    spin_unlock_irqrestore(&v->arch.vgic.lock, flags);
+    if ( p->desc != NULL )
+    {
+        spin_lock_irqsave(&p->desc->lock, flags);
+        p->desc->handler->enable(p->desc);
+        spin_unlock_irqrestore(&p->desc->lock, flags);
+    }
+}
 /* ITS device table helper functions */
 int vits_vdevice_entry(struct domain *d, uint32_t dev_id,
                        struct vdevice_table *entry, int set)
@@ -649,6 +689,482 @@ err:
     return 0;
 }
 
+static int vgic_v3_gits_lpi_mmio_read(struct vcpu *v, mmio_info_t *info)
+{
+    uint32_t offset;
+    struct hsr_dabt dabt = info->dabt;
+    struct cpu_user_regs *regs = guest_cpu_user_regs();
+    register_t *r = select_user_reg(regs, dabt.reg);
+    uint8_t cfg;
+
+    offset = info->gpa -
+             (v->domain->arch.vits->propbase & 0xfffffffff000UL);
+
+    if ( offset < SZ_64K )
+    {
+        DPRINTK("vITS:d%dv%d LPI Table read offset 0x%x\n",
+                v->domain->domain_id, v->vcpu_id, offset);
+        cfg = readb_relaxed(v->domain->arch.vits->prop_page + offset);
+        *r = cfg;
+        return 1;
+    }
+    else
+        dprintk(XENLOG_G_ERR, "vITS:d%dv%d LPI Table read with wrong offset 0x%x\n",
+                v->domain->domain_id, v->vcpu_id, offset);
+
+
+    return 0;
+}
+
+static int vgic_v3_gits_lpi_mmio_write(struct vcpu *v, mmio_info_t *info)
+{
+    uint32_t offset;
+    uint32_t vid;
+    uint8_t cfg;
+    bool_t enable;
+    struct hsr_dabt dabt = info->dabt;
+    struct cpu_user_regs *regs = guest_cpu_user_regs();
+    register_t *r = select_user_reg(regs, dabt.reg);
+
+    offset = info->gpa -
+             (v->domain->arch.vits->propbase & 0xfffffffff000UL);
+
+    vid = offset + NR_GIC_LPI;
+    if ( offset < SZ_64K )
+    {
+        DPRINTK("vITS:d%dv%d LPI Table write offset 0x%x\n",
+                v->domain->domain_id, v->vcpu_id, offset);
+        cfg = readb_relaxed(v->domain->arch.vits->prop_page + offset);
+        enable = (cfg & *r) & 0x1;
+
+        if ( !enable )
+             vgic_its_enable_lpis(v, vid,  (*r & 0xfc));
+        else
+             vgic_its_disable_lpis(v, vid);
+
+        /* Update virtual prop page */
+        writeb_relaxed((*r & 0xff),
+                        v->domain->arch.vits->prop_page + offset);
+        
+        return 1;
+    }
+    else
+        dprintk(XENLOG_G_ERR, "vITS:d%dv%d LPI Table invalid write @ 0x%x\n",
+                v->domain->domain_id, v->vcpu_id, offset);
+
+    return 0; 
+}
+
+static const struct mmio_handler_ops vgic_gits_lpi_mmio_handler = {
+    .read_handler  = vgic_v3_gits_lpi_mmio_read,
+    .write_handler = vgic_v3_gits_lpi_mmio_write,
+};
+
+int vgic_its_unmap_lpi_prop(struct vcpu *v)
+{
+    paddr_t maddr;
+    uint32_t lpi_size;
+    int i;
+    
+    maddr = v->domain->arch.vits->propbase & 0xfffffffff000UL;
+    lpi_size = 1UL << ((v->domain->arch.vits->propbase & 0x1f) + 1);
+
+    DPRINTK("vITS:d%dv%d Unmap guest LPI conf table maddr 0x%lx lpi_size 0x%x\n", 
+             v->domain->domain_id, v->vcpu_id, maddr, lpi_size);
+
+    if ( lpi_size < SZ_64K )
+    {
+        dprintk(XENLOG_G_ERR, "vITS:d%dv%d LPI Prop page < 64K\n",
+                v->domain->domain_id, v->vcpu_id);
+        return 0;
+    }
+
+    /* XXX: As per 4.8.9 each re-distributor shares a common LPI configuration table 
+     * So one set of mmio handlers to manage configuration table is enough
+     */
+    for ( i = 0; i < lpi_size / PAGE_SIZE; i++ )
+        guest_physmap_remove_page(v->domain, paddr_to_pfn(maddr),
+                                gmfn_to_mfn(v->domain, paddr_to_pfn(maddr)), 0);
+
+    /* Register mmio handlers for this region */
+    register_mmio_handler(v->domain, &vgic_gits_lpi_mmio_handler,
+                          maddr, lpi_size);
+
+    /* Allocate Virtual LPI Property table */
+    v->domain->arch.vits->prop_page =
+        alloc_xenheap_pages(get_order_from_bytes(lpi_size), 0);
+    if ( !v->domain->arch.vits->prop_page )
+    {
+        dprintk(XENLOG_G_ERR, "vITS:d%dv%d Failed to allocate LPI Prop page\n",
+                v->domain->domain_id, v->vcpu_id);
+        return 0;
+    }
+
+    memset(v->domain->arch.vits->prop_page, 0xa2, lpi_size);
+
+    return 1;
+}
+
+static inline void vits_spin_lock(struct vgic_its *vits)
+{
+    spin_lock(&vits->lock);
+}
+
+static inline void vits_spin_unlock(struct vgic_its *vits)
+{
+    spin_unlock(&vits->lock);
+}
+
+static int vgic_v3_gits_mmio_read(struct vcpu *v, mmio_info_t *info)
+{
+    struct vgic_its *vits;
+    struct hsr_dabt dabt = info->dabt;
+    struct cpu_user_regs *regs = guest_cpu_user_regs();
+    register_t *r = select_user_reg(regs, dabt.reg);
+    uint64_t val = 0;
+    uint32_t index, gits_reg;
+
+    vits = v->domain->arch.vits;
+
+    gits_reg = info->gpa - vits->phys_base;
+
+    if ( gits_reg >= SZ_64K )
+    {
+        gdprintk(XENLOG_G_WARNING,
+                 "vITS:d%dv%d unknown gpa read address %"PRIpaddr"\n",
+                 v->domain->domain_id, v->vcpu_id, info->gpa);
+        return 0;
+    }
+
+    switch ( gits_reg )
+    {
+    case GITS_CTLR:
+        if ( dabt.size != DABT_WORD )
+            goto bad_width;
+        *r = 0;
+        return 1;
+    case GITS_IIDR:
+        if ( dabt.size != DABT_WORD )
+            goto bad_width;
+        *r = 0;
+        return 1;
+    case GITS_TYPER:
+        vits_spin_lock(vits);
+        val = (((v->domain->max_vcpus + 1) << GITS_TYPER_HCC_SHIFT ) |
+                 VITS_GITS_DEV_BITS | VITS_GITS_ID_BITS              |
+                 VITS_GITS_ITT_SIZE | VITS_GITS_DISTRIBUTED          |
+                 VITS_GITS_PLPIS);
+        if ( dabt.size == DABT_DOUBLE_WORD )
+            *r = val;
+        else if ( dabt.size == DABT_WORD )
+            *r = (u32)val;
+        else
+        {
+            vits_spin_unlock(vits);
+            goto bad_width;
+        }
+        vits_spin_unlock(vits);
+        return 1;
+    case GITS_TYPER + 4:
+        if (dabt.size != DABT_WORD ) goto bad_width;
+        vits_spin_lock(vits);
+        val = (((v->domain->max_vcpus + 1) << GITS_TYPER_HCC_SHIFT ) |
+                 VITS_GITS_DEV_BITS | VITS_GITS_ID_BITS              |
+                 VITS_GITS_ITT_SIZE | VITS_GITS_DISTRIBUTED          |
+                 VITS_GITS_PLPIS);
+        *r = (u32)(val >> 32);
+        vits_spin_unlock(vits);
+        return 1;
+    case 0x0010 ... 0x007c:
+    case 0xc000 ... 0xffcc:
+        /* Implementation defined -- read ignored */
+        dprintk(XENLOG_ERR,
+                "vITS:d%dv%d read unknown 0x000c - 0x007c r%d offset %#08x\n",
+                v->domain->domain_id, v->vcpu_id, dabt.reg, gits_reg);
+        goto read_as_zero;
+    case GITS_CBASER:
+        /* XXX: Only read support 32/64-bit access */
+        vits_spin_lock(vits);
+        if ( dabt.size == DABT_DOUBLE_WORD )
+            *r = vits->cmd_base && 0xc7ffffffffffffffUL;
+        else if ( dabt.size == DABT_WORD )
+            *r = (u32)vits->cmd_base;
+        else
+        {
+            vits_spin_unlock(vits);
+            goto bad_width;
+        }
+        vits_spin_unlock(vits);
+        return 1;
+    case GITS_CBASER + 4:
+        if (dabt.size != DABT_WORD )
+            goto bad_width;
+        vits_spin_lock(vits);
+        *r = (u32)(vits->cmd_base >> 32);
+        vits_spin_unlock(vits);
+        return 1;
+    case GITS_CWRITER:
+        /* XXX: Only read support 32/64-bit access */
+        vits_spin_lock(vits);
+        if ( dabt.size == DABT_DOUBLE_WORD )
+            *r = vits->cmd_write;
+        else if ( dabt.size == DABT_WORD )
+            *r = (u32)vits->cmd_write;
+        else
+        {
+            vits_spin_unlock(vits);
+            goto bad_width;
+        }
+        vits_spin_unlock(vits);
+        return 1;
+    case GITS_CWRITER + 4:
+        if ( dabt.size != DABT_WORD )
+            goto bad_width;
+        vits_spin_lock(vits);
+        *r = (u32)(vits->cmd_write >> 32);
+        vits_spin_unlock(vits);
+        return 1;
+    case GITS_CREADR:
+        /* XXX: Only read support 32/64-bit access */
+        vits_spin_lock(vits);
+        if ( dabt.size == DABT_DOUBLE_WORD )
+            *r = vits->cmd_read;
+        else if ( dabt.size == DABT_WORD )
+            *r = (u32)vits->cmd_read;
+        else
+        {
+            vits_spin_unlock(vits);
+            goto bad_width;
+        }
+        vits_spin_unlock(vits);
+        return 1;
+    case GITS_CREADR + 4:
+        if ( dabt.size != DABT_WORD )
+            goto bad_width;
+        vits_spin_lock(vits);
+        *r = (u32)(vits->cmd_read >> 32);
+        vits_spin_unlock(vits);
+        return 1;
+    case 0x0098 ... 0x009c:
+    case 0x00a0 ... 0x00fc:
+    case 0x0140 ... 0xbffc:
+        /* Reserved -- read ignored */
+        dprintk(XENLOG_ERR,
+                "vITS:d%dv%d read unknown 0x0098-9c or 0x00a0-fc r%d offset %#08x\n",
+                v->domain->domain_id, v->vcpu_id, dabt.reg, gits_reg);
+        goto read_as_zero;
+    case GITS_BASER ... GITS_BASERN:
+        /* Supports only 64-bit access */
+        if ( dabt.size != DABT_DOUBLE_WORD )
+            goto bad_width;
+        if ( (gits_reg % 8) != 0 )
+            goto bad_width;
+        vits_spin_lock(vits);
+        index = (gits_reg - GITS_BASER) / 8;
+        *r = vits->baser[index];
+        vits_spin_unlock(vits);
+        return 1;
+    case GITS_PIDR0:
+        if ( dabt.size != DABT_WORD )
+            goto bad_width;
+        *r = GITS_PIDR0_VAL;
+        return 1;
+    case GITS_PIDR1:
+        if ( dabt.size != DABT_WORD )
+            goto bad_width;
+        *r = GITS_PIDR1_VAL;
+        return 1;
+    case GITS_PIDR2:
+        if ( dabt.size != DABT_WORD )
+            goto bad_width;
+        *r = GITS_PIDR2_VAL;
+        return 1;
+    case GITS_PIDR3:
+        if ( dabt.size != DABT_WORD )
+            goto bad_width;
+        *r = GITS_PIDR3_VAL;
+        return 1;
+    case GITS_PIDR4:
+        if ( dabt.size != DABT_WORD )
+            goto bad_width;
+        *r = GITS_PIDR4_VAL;
+        return 1;
+    case GITS_PIDR5 ... GITS_PIDR7:
+        goto read_as_zero;
+   default:
+        dprintk(XENLOG_G_ERR, "vITS:d%dv%d unhandled read r%d offset %#08x\n",
+                v->domain->domain_id, v->vcpu_id, dabt.reg, gits_reg);
+        return 0;
+    }
+
+bad_width:
+    dprintk(XENLOG_G_ERR, "vITS:d%dv%d bad read width %d r%d offset %#08x\n",
+           v->domain->domain_id, v->vcpu_id, dabt.size, dabt.reg, gits_reg);
+    domain_crash_synchronous();
+    return 0;
+
+read_as_zero:
+    if ( dabt.size != DABT_WORD )
+       goto bad_width;
+    *r = 0;
+    return 1;
+}
+
+#define GITS_BASER_MASK  (~((0x7UL << GITS_BASER_TYPE_SHIFT) | \
+                         (0xffUL << GITS_BASER_ENTRY_SIZE_SHIFT) | \
+                         (0x3UL << GITS_BASER_SHAREABILITY_SHIFT)))
+
+static int vgic_v3_gits_mmio_write(struct vcpu *v, mmio_info_t *info)
+{
+    struct vgic_its *vits;
+    struct hsr_dabt dabt = info->dabt;
+    struct cpu_user_regs *regs = guest_cpu_user_regs();
+    register_t *r = select_user_reg(regs, dabt.reg);
+    int ret;
+    uint32_t index, gits_reg, sz, psz;
+    uint64_t val;
+
+    vits = v->domain->arch.vits;
+
+    gits_reg = info->gpa - vits->phys_base;
+
+    if ( gits_reg >= SZ_64K )
+    {
+        gdprintk(XENLOG_G_WARNING,
+                 "vITS:d%dv%d unknown gpa write address %"PRIpaddr"\n",
+                 v->domain->domain_id, v->vcpu_id, info->gpa);
+        return 0;
+    }
+    switch ( gits_reg )
+    {
+    case GITS_CTLR:
+        if ( dabt.size != DABT_WORD )
+            goto bad_width;
+        vits_spin_lock(vits);
+        vits->ctrl = *r;
+        vits_spin_unlock(vits);
+        return 1;
+    case GITS_IIDR:
+        /* R0 -- write ignored */
+        goto write_ignore;
+    case GITS_TYPER:
+    case GITS_TYPER + 4:
+        /* R0 -- write ignored */
+        goto write_ignore;
+    case 0x0010 ... 0x007c:
+    case 0xc000 ... 0xffcc:
+        /* Implementation defined -- write ignored */
+        dprintk(XENLOG_G_ERR,
+                "vITS:d%dv%d write to unknown 0x000c - 0x007c r%d offset %#08x\n",
+                v->domain->domain_id, v->vcpu_id, dabt.reg, gits_reg);
+        goto write_ignore;
+    case GITS_CBASER:
+        if ( dabt.size != DABT_DOUBLE_WORD )
+            goto bad_width;
+        vits_spin_lock(vits);
+        vits->cmd_base = *r;
+        vits->cmd_qsize  =  SZ_4K * ((*r & 0xff) + 1);
+        vits_spin_unlock(vits);
+        return 1;
+    case GITS_CBASER + 4:
+         /* XXX: Does not support word write */
+        goto bad_width;
+    case GITS_CWRITER:
+        vits_spin_lock(vits);
+        if ( dabt.size == DABT_DOUBLE_WORD )
+            vits->cmd_write = *r;
+        else if ( dabt.size == DABT_WORD)
+        {
+            val = vits->cmd_write & 0xffffffff00000000UL;
+            val = (*r) | val;
+            vits->cmd_write =  val;
+        }
+        else
+        {
+            vits_spin_unlock(vits);
+            goto bad_width;
+        }
+        ret = vgic_its_process_cmd(v, vits);
+        vits_spin_unlock(vits);
+        return ret;
+    case GITS_CWRITER + 4:
+        if (dabt.size != DABT_WORD )
+            goto bad_width;
+        vits_spin_lock(vits);
+        val = vits->cmd_write & 0xffffffffUL;
+        val = ((*r & 0xffffffffUL) << 32) | val;
+        vits->cmd_write =  val;
+        ret = vgic_its_process_cmd(v, vits);
+        vits_spin_unlock(vits);
+        return ret;
+    case GITS_CREADR:
+        /* R0 -- write ignored */
+        goto write_ignore;
+    case 0x0098 ... 0x009c:
+    case 0x00a0 ... 0x00fc:
+    case 0x0140 ... 0xbffc:
+        /* Reserved -- write ignored */
+        dprintk(XENLOG_G_ERR,
+                "vITS:d%dv%d write to unknown 0x98-9c or 0xa0-fc r%d offset %#08x\n",
+                v->domain->domain_id, v->vcpu_id, dabt.reg, gits_reg);
+        goto write_ignore;
+    case GITS_BASER0:
+        if ( dabt.size != DABT_DOUBLE_WORD )
+            goto bad_width;
+        vits_spin_lock(vits);
+        vits->baser[0] = vits->baser[0] | (GITS_BASER_MASK & *r);
+        vits->dt_ipa = vits->baser[0] & 0xfffffffff000UL;
+        psz = (vits->baser[0] >> GITS_BASER_PAGE_SIZE_SHIFT) &
+               GITS_BASER_PAGE_SIZE_MASK_VAL;
+        if ( psz == GITS_BASER_PAGE_SIZE_4K_VAL )
+            sz = 4;
+        else if ( psz == GITS_BASER_PAGE_SIZE_16K_VAL )
+            sz = 16;
+        else
+            sz = 64;
+
+        vits->dt_size = (vits->baser[0] & GITS_BASER_PAGES_MASK_VAL)
+                        * sz * SZ_1K;
+        vits_spin_unlock(vits);
+        return 1;
+    case GITS_BASER1 ... GITS_BASERN:
+        /* Nothing to do with this values. Just store and emulate */
+        if ( dabt.size != DABT_DOUBLE_WORD )
+            goto bad_width;
+        if ( (gits_reg % 8) != 0 )
+            goto bad_width;
+        vits_spin_lock(vits);
+        index = (gits_reg - GITS_BASER) / 8;
+        vits->baser[index] = *r;
+        vits_spin_unlock(vits);
+        return 1;
+    case GITS_PIDR7 ... GITS_PIDR0:
+        /* R0 -- write ignored */
+        goto write_ignore;
+   default:
+        dprintk(XENLOG_G_ERR, "vITS:d%dv%d unhandled write r%d offset %#08x\n",
+                v->domain->domain_id, v->vcpu_id, dabt.reg, gits_reg);
+        return 0;
+    }
+
+bad_width:
+    dprintk(XENLOG_G_ERR, "vITS:d%dv%d bad write width %d r%d offset %#08x\n",
+            v->domain->domain_id, v->vcpu_id, dabt.size, dabt.reg, gits_reg);
+    domain_crash_synchronous();
+    return 0;
+
+write_ignore:
+    if ( dabt.size != DABT_WORD ) goto bad_width;
+    *r = 0;
+    return 1;
+}
+
+
+static const struct mmio_handler_ops vgic_gits_mmio_handler = {
+    .read_handler  = vgic_v3_gits_mmio_read,
+    .write_handler = vgic_v3_gits_mmio_write,
+};
+
 /*
  * Local variables:
  * mode: C
xen/include/asm-arm/gic-its.h +14 -0
index 8f898a6..3271477 100644
@@ -38,6 +38,8 @@ struct its_collection {
 struct vgic_its
 {
    spinlock_t lock;
+   /* Emulation of BASER */
+   paddr_t baser[8];
    /* Command queue base */
    paddr_t cmd_base;
    /* Command queue write pointer */
@@ -48,8 +50,20 @@ struct vgic_its
    paddr_t cmd_read;
    /* Command queue size */
    unsigned long cmd_qsize;
+   /* ITS mmio physical base */
+   paddr_t phys_base;
+   /* ITS mmio physical size */
+   unsigned long phys_size;
    /* ITS physical node */
    struct its_node *its;
+   /* GICR ctrl register */
+   uint32_t ctrl;
+   /* LPI propbase */
+   paddr_t propbase;
+   /* percpu pendbase */
+   paddr_t pendbase[MAX_VIRT_CPUS];
+   /* Virtual LPI property table */
+   void * prop_page;
    /* vITT device table ipa */
    paddr_t dt_ipa;
    /* vITT device table size */
#63 lore raw

RFC PATCH v3 12/18 xen/arm: ITS: Add GICR register emulation

In reply to [email protected]

From: Vijaya Kumar K <[email protected]>

Emulate LPI related changes to GICR registers

---
 xen/arch/arm/gic-v3.c             |   15 ++++++++
 xen/arch/arm/gic.c                |   10 ++++++
 xen/arch/arm/vgic-v3.c            |   69 ++++++++++++++++++++++++++++++-------
 xen/include/asm-arm/domain.h      |    1 +
 xen/include/asm-arm/gic-its.h     |    1 +
 xen/include/asm-arm/gic.h         |    9 +++++
 xen/include/asm-arm/gic_v3_defs.h |    1 +
 7 files changed, 94 insertions(+), 12 deletions(-)
xen/arch/arm/gic-v3.c +15 -0
index 556b291..737646c 100644
@@ -683,6 +683,11 @@ static int __init gicv3_populate_rdist(void)
     return -ENODEV;
 }
 
+static int gicv3_dist_supports_lpis(void)
+{
+    return readl_relaxed(GICD + GICD_TYPER) & GICD_TYPER_LPIS_SUPPORTED;
+}
+
 static int __cpuinit gicv3_cpu_init(void)
 {
     int i;
@@ -1304,10 +1309,20 @@ static int __init gicv3_init(void)
            gicv3.rdist_regions[0].size, gicv3.rdist_regions[0].map_base,
            gicv3_info.maintenance_irq);
 
+    reg = readl_relaxed(GICD + GICD_TYPER);
+
+    gicv3.rdist_data.id_bits = ((reg >> 19) & 0x1f) + 1;
+    gicv3_info.nr_id_bits = gicv3.rdist_data.id_bits;
+
     spin_lock_init(&gicv3.lock);
 
     spin_lock(&gicv3.lock);
 
+    if ( gicv3_dist_supports_lpis() )
+        gicv3_info.lpi_supported = 1;
+    else
+        gicv3_info.lpi_supported = 0;
+
     gicv3_dist_init();
     res = gicv3_cpu_init();
     gicv3_hyp_init();
xen/arch/arm/gic.c +10 -0
index c41e82e..cfc9c42 100644
@@ -67,6 +67,16 @@ unsigned int gic_number_lines(void)
     return gic_hw_ops->info->nr_lines;
 }
 
+unsigned int gic_nr_id_bits(void)
+{
+    return gic_hw_ops->info->nr_id_bits;
+}
+
+bool_t gic_lpi_supported(void)
+{
+    return gic_hw_ops->info->lpi_supported;
+}
+
 void gic_save_state(struct vcpu *v)
 {
     ASSERT(!local_irq_is_enabled());
xen/arch/arm/vgic-v3.c +57 -12

113 lines not on this page · raw message

xen/include/asm-arm/domain.h +1 -0
index da73cf5..db1d1db 100644
@@ -102,6 +102,7 @@ struct arch_domain
         paddr_t dbase; /* Distributor base address */
         paddr_t cbase; /* CPU base address */
 #ifdef CONFIG_ARM_64
+	int gicr_ctlr;
         /* GIC V3 addressing */
         paddr_t dbase_size; /* Distributor base size */
         /* List of contiguous occupied by the redistributors */
xen/include/asm-arm/gic-its.h +1 -0
index 3271477..1de57a7 100644
@@ -277,6 +277,7 @@ static inline uint32_t its_decode_devid(struct domain *d, its_cmd_block *cmd)
 void its_set_affinity(struct irq_desc *desc, int cpu);
 void lpi_set_config(struct irq_desc *desc, int enable);
 uint32_t its_get_nr_events(void);
+int vgic_its_unmap_lpi_prop(struct vcpu *v);
 struct vits_device * find_vits_device(struct rb_root *root, uint32_t devid);
 int insert_vits_device(struct rb_root *root, struct vits_device *dev);
 int remove_vits_device(struct rb_root *root, struct vits_device *dev);
xen/include/asm-arm/gic.h +9 -0
index 0209cc5..ee612de 100644
@@ -99,6 +99,7 @@
 #define GICD_TYPE_CPUS_SHIFT 5
 #define GICD_TYPE_CPUS  0x0e0
 #define GICD_TYPE_SEC   0x400
+#define GICD_TYPE_LPIS  (0x1UL << 17)
 
 #define GICC_CTL_ENABLE 0x1
 #define GICC_CTL_EOI    (0x1 << 9)
@@ -282,6 +283,10 @@ extern void gic_dump_info(struct vcpu *v);
 
 /* Number of interrupt lines */
 extern unsigned int gic_number_lines(void);
+/* Number of interrupt id bits supported */
+extern unsigned int gic_nr_id_bits(void);
+/* LPI support info */
+bool_t gic_lpi_supported(void);
 
 /* IRQ translation function for the device tree */
 int gic_irq_xlate(const u32 *intspec, unsigned int intsize,
@@ -299,6 +304,10 @@ struct gic_info {
     unsigned int maintenance_irq;
     /* Pointer to the device tree node representing the interrupt controller */
     const struct dt_device_node *node;
+    /* Number of IRQ ID bits supported */
+    uint32_t nr_id_bits;
+    /* LPIs are support information */
+    bool_t lpi_supported; 
 };
 
 struct gic_hw_operations {
xen/include/asm-arm/gic_v3_defs.h +1 -0
index dc4fe14..0872097 100644
@@ -136,6 +136,7 @@
 #define GICR_TYPER_VLPIS             (1U << 1)
 #define GICR_TYPER_DISTRIBUTED_IMP   (1U << 3)
 #define GICR_TYPER_LAST              (1U << 4)
+#define GICR_TYPER_PROCESSOR_SHIFT   (8)
 
 #define DEFAULT_PMR_VALUE            0xff
 
Signature · 1 line
1.7.9.5
#64 lore raw

RFC PATCH v3 13/18 xen/arm: ITS: Add irq descriptors for LPIs

In reply to [email protected]

From: Vijaya Kumar K <[email protected]>

Add irq descriptors for LPIs and route

---
 xen/arch/arm/gic-v3.c         |    8 +++-
 xen/arch/arm/gic.c            |   17 +++++++-
 xen/arch/arm/irq.c            |   38 +++++++++++++----
 xen/arch/arm/vgic-v3-its.c    |    9 +++++
 xen/arch/arm/vgic.c           |   90 ++++++++++++++++++++++++++++++++++++++---
 xen/include/asm-arm/domain.h  |    2 +
 xen/include/asm-arm/gic-its.h |    6 +++
 xen/include/asm-arm/gic.h     |    3 ++
 xen/include/asm-arm/vgic.h    |    1 +
 9 files changed, 157 insertions(+), 17 deletions(-)
xen/arch/arm/gic-v3.c +6 -2
index 737646c..793f2f0 100644
@@ -899,9 +899,13 @@ static void gicv3_update_lr(int lr, const struct pending_irq *p,
 
     val =  (((uint64_t)state & 0x3) << GICH_LR_STATE_SHIFT) | grp;
     val |= ((uint64_t)p->priority & 0xff) << GICH_LR_PRIORITY_SHIFT;
-    val |= ((uint64_t)p->irq & GICH_LR_VIRTUAL_MASK) << GICH_LR_VIRTUAL_SHIFT;
 
-   if ( p->desc != NULL )
+    if ( is_lpi(p->irq) )
+        val |= ((uint64_t)p->irq & GICH_LR_VIRTUAL_MASK) << GICH_LR_VIRTUAL_SHIFT;
+    else
+        val |= ((uint64_t)p->irq & GICH_LR_VIRTUAL_MASK) << GICH_LR_VIRTUAL_SHIFT;
+
+   if ( p->desc != NULL && !(is_lpi(p->irq)) )
        val |= GICH_LR_HW | (((uint64_t)p->desc->irq & GICH_LR_PHYSICAL_MASK)
                            << GICH_LR_PHYSICAL_SHIFT);
 
xen/arch/arm/gic.c +15 -2
index cfc9c42..091f7e5 100644
@@ -124,18 +124,31 @@ void gic_route_irq_to_xen(struct irq_desc *desc, const cpumask_t *cpu_mask,
                           unsigned int priority)
 {
     ASSERT(priority <= 0xff);     /* Only 8 bits of priority */
-    ASSERT(desc->irq < gic_number_lines());/* Can't route interrupts that don't exist */
+    /* Can't route interrupts that don't exist */
+    ASSERT(desc->irq < gic_number_lines() && is_lpi(desc->irq));
     ASSERT(test_bit(_IRQ_DISABLED, &desc->status));
     ASSERT(spin_is_locked(&desc->lock));
 
     desc->handler = gic_hw_ops->gic_host_irq_type;
 
-    gic_set_irq_properties(desc, cpu_mask, priority);
+    if ( !is_lpi(desc->irq) )
+        gic_set_irq_properties(desc, cpu_mask, priority);
 }
 
 /* Program the GIC to route an interrupt to a guest
  *   - desc.lock must be held
  */
+int gic_route_lpi_to_guest(struct domain *d, unsigned int virq,
+                           struct irq_desc *desc, unsigned int priority)
+{
+    ASSERT(spin_is_locked(&desc->lock));
+
+    desc->handler = gic_hw_ops->gic_guest_irq_type;
+    set_bit(_IRQ_GUEST, &desc->status);
+
+    return 0;
+}
+
 int gic_route_irq_to_guest(struct domain *d, unsigned int virq,
                            struct irq_desc *desc, unsigned int priority)
 {
xen/arch/arm/irq.c +31 -7
index 9dbdf7d..105ef85 100644
@@ -57,12 +57,22 @@ hw_irq_controller no_irq_type = {
 };
 
 static irq_desc_t irq_desc[NR_IRQS];
+static irq_desc_t irq_desc_lpi[MAX_NR_LPIS];
 static DEFINE_PER_CPU(irq_desc_t[NR_LOCAL_IRQS], local_irq_desc);
 
 irq_desc_t *__irq_to_desc(int irq)
 {
+    struct irq_desc *desc = NULL;
     if (irq < NR_LOCAL_IRQS) return &this_cpu(local_irq_desc)[irq];
-    return &irq_desc[irq-NR_LOCAL_IRQS];
+    else if ( irq >= NR_LOCAL_IRQS && irq < NR_IRQS)
+        return &irq_desc[irq-NR_LOCAL_IRQS];
+    else
+    {
+        if ( is_lpi(irq) )
+            return &irq_desc_lpi[irq - NR_GIC_LPI];
+    }
+
+    return desc;
 }
 
 int __init arch_init_one_irq_desc(struct irq_desc *desc)
@@ -83,6 +93,13 @@ static int __init init_irq_data(void)
         desc->action  = NULL;
     }
 
+    for ( irq = NR_GIC_LPI; irq < MAX_LPI; irq++ )
+    {
+        struct irq_desc *desc = irq_to_desc(irq);
+        init_one_irq_desc(desc);
+        desc->irq = irq;
+        desc->action  = NULL;
+    }
     return 0;
 }
 
@@ -178,7 +195,7 @@ int request_irq(unsigned int irq, unsigned int irqflags,
      * which interrupt is which (messes up the interrupt freeing
      * logic etc).
      */
-    if ( irq >= nr_irqs )
+    if ( irq >= nr_irqs && !is_lpi(irq) )
         return -EINVAL;
     if ( !handler )
         return -EINVAL;
@@ -237,9 +254,12 @@ void do_IRQ(struct cpu_user_regs *regs, unsigned int irq, int is_fiq)
         set_bit(_IRQ_INPROGRESS, &desc->status);
         desc->arch.eoi_cpu = smp_processor_id();
 
+        if ( is_lpi(irq) )
+            vgic_vcpu_inject_lpi(info->d, irq);
+        else
         /* the irq cannot be a PPI, we only support delivery of SPIs to
          * guests */
-        vgic_vcpu_inject_spi(info->d, info->virq);
+            vgic_vcpu_inject_spi(info->d, info->virq);
         goto out_no_end;
     }
 
@@ -405,6 +425,8 @@ err:
 
 bool_t is_assignable_irq(unsigned int irq)
 {
+    if ( is_lpi(irq) )
+        return 1;
     /* For now, we can only route SPIs to the guest */
     return ((irq >= NR_LOCAL_IRQS) && (irq < gic_number_lines()));
 }
@@ -422,7 +444,7 @@ int route_irq_to_guest(struct domain *d, unsigned int virq,
     unsigned long flags;
     int retval = 0;
 
-    if ( virq >= vgic_num_irqs(d) )
+    if ( virq >= vgic_num_irqs(d) && !is_lpi(irq) )
     {
         printk(XENLOG_G_ERR
                "the vIRQ number %u is too high for domain %u (max = %u)\n",
@@ -431,7 +453,7 @@ int route_irq_to_guest(struct domain *d, unsigned int virq,
     }
 
     /* Only routing to virtual SPIs is supported */
-    if ( virq < NR_LOCAL_IRQS )
+    if ( virq < NR_LOCAL_IRQS && !is_lpi(irq) )
     {
         printk(XENLOG_G_ERR "IRQ can only be routed to an SPI\n");
         return -EINVAL;
@@ -507,8 +529,10 @@ int route_irq_to_guest(struct domain *d, unsigned int virq,
     retval = __setup_irq(desc, 0, action);
     if ( retval )
         goto out;
-
-    retval = gic_route_irq_to_guest(d, virq, desc, GIC_PRI_IRQ);
+    if ( is_lpi(irq) )
+        retval = gic_route_lpi_to_guest(d, virq, desc, GIC_PRI_IRQ);
+    else
+        retval = gic_route_irq_to_guest(d, virq, desc, GIC_PRI_IRQ);
 
     spin_unlock_irqrestore(&desc->lock, flags);
 
xen/arch/arm/vgic-v3-its.c +9 -0
index fa9dccc..543db91 100644
@@ -689,6 +689,15 @@ err:
     return 0;
 }
 
+uint8_t vgic_its_get_priority(struct vcpu *v, uint32_t pid)
+{
+    uint8_t priority;
+
+    priority =  readb_relaxed(v->domain->arch.vits->prop_page + pid);
+    priority &= 0xfc;
+
+    return priority;
+}
 static int vgic_v3_gits_lpi_mmio_read(struct vcpu *v, mmio_info_t *info)
 {
     uint32_t offset;
xen/arch/arm/vgic.c +84 -6

129 lines not on this page · raw message

xen/include/asm-arm/domain.h +2 -0
index db1d1db..06105cc 100644
@@ -98,6 +98,8 @@ struct arch_domain
          * struct arch_vcpu.
          */
         struct pending_irq *pending_irqs;
+        struct pending_irq *pending_lpis;
+
         /* Base address for guest GIC */
         paddr_t dbase; /* Distributor base address */
         paddr_t cbase; /* CPU base address */
xen/include/asm-arm/gic-its.h +6 -0
index 1de57a7..f34a207 100644
@@ -276,11 +276,17 @@ static inline uint32_t its_decode_devid(struct domain *d, its_cmd_block *cmd)
 
 void its_set_affinity(struct irq_desc *desc, int cpu);
 void lpi_set_config(struct irq_desc *desc, int enable);
+uint8_t vgic_its_get_priority(struct vcpu *v, uint32_t pid);
+
 uint32_t its_get_nr_events(void);
 int vgic_its_unmap_lpi_prop(struct vcpu *v);
 struct vits_device * find_vits_device(struct rb_root *root, uint32_t devid);
 int insert_vits_device(struct rb_root *root, struct vits_device *dev);
 int remove_vits_device(struct rb_root *root, struct vits_device *dev);
+int vits_get_vitt_entry(struct domain *d, uint32_t devid,
+                        uint32_t event, struct vitt *entry);
+int vits_get_vdevice_entry(struct domain *d, uint32_t devid,
+                           struct vdevice_table *entry);
 #endif /* __ASM_ARM_GIC_ITS_H__ */
 
 /*
xen/include/asm-arm/gic.h +3 -0
index ee612de..9e94d65 100644
@@ -225,6 +225,9 @@ extern void gic_route_irq_to_xen(struct irq_desc *desc, const cpumask_t *cpu_mas
 extern int gic_route_irq_to_guest(struct domain *, unsigned int virq,
                                   struct irq_desc *desc,
                                   unsigned int priority);
+extern int gic_route_lpi_to_guest(struct domain *d, unsigned int virq,
+                                   struct irq_desc *desc,
+                                   unsigned int priority);
 
 /* Remove an IRQ passthrough to a guest */
 int gic_remove_irq_from_guest(struct domain *d, unsigned int virq,
xen/include/asm-arm/vgic.h +1 -0
index 8d22532..f8928ab 100644
@@ -183,6 +183,7 @@ extern int vcpu_vgic_init(struct vcpu *v);
 extern struct vcpu *vgic_get_target_vcpu(struct vcpu *v, unsigned int irq);
 extern void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq);
 extern void vgic_vcpu_inject_spi(struct domain *d, unsigned int virq);
+extern void vgic_vcpu_inject_lpi(struct domain *d, unsigned int virq);
 extern void vgic_clear_pending_irqs(struct vcpu *v);
 extern struct pending_irq *irq_to_pending(struct vcpu *v, unsigned int irq);
 extern struct pending_irq *spi_to_pending(struct domain *d, unsigned int irq);
Signature · 1 line
1.7.9.5
#65 lore raw

Re: [RFC PATCH v3 13/18] xen/arm: ITS: Add irq descriptors for LPIs

Quoted 3 lines · On Mon, 2015-06-22 at 17:31 +0530, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

Add irq descriptors for LPIs and route

This seems to also do interrupt injection for LPIs and more. Please check that your commit messages are accurately describing the contents of the patch. If it turns into a long list of unrelated sounding things then that might suggest the patch should be split up.

Quoted 18 lines
---
 xen/arch/arm/gic-v3.c         |    8 +++-
 xen/arch/arm/gic.c            |   17 +++++++-
 xen/arch/arm/irq.c            |   38 +++++++++++++----
 xen/arch/arm/vgic-v3-its.c    |    9 +++++
 xen/arch/arm/vgic.c           |   90 ++++++++++++++++++++++++++++++++++++++---
 xen/include/asm-arm/domain.h  |    2 +
 xen/include/asm-arm/gic-its.h |    6 +++
 xen/include/asm-arm/gic.h     |    3 ++
 xen/include/asm-arm/vgic.h    |    1 +
 9 files changed, 157 insertions(+), 17 deletions(-)
xen/arch/arm/gic-v3.c +0 -0
index 737646c..793f2f0 100644

xen/arch/arm/gic-v3.c static void gicv3_update_lr(int lr, const struct pending_irq *p, · line 906

Quoted 10 lines
xen/arch/arm/gic-v3.c +4 -2
@@ -899,9 +899,13 @@ static void gicv3_update_lr(int lr, const struct pending_irq *p,
 
     val =  (((uint64_t)state & 0x3) << GICH_LR_STATE_SHIFT) | grp;
     val |= ((uint64_t)p->priority & 0xff) << GICH_LR_PRIORITY_SHIFT;
-    val |= ((uint64_t)p->irq & GICH_LR_VIRTUAL_MASK) << GICH_LR_VIRTUAL_SHIFT;
 
-   if ( p->desc != NULL )
+    if ( is_lpi(p->irq) )
+        val |= ((uint64_t)p->irq & GICH_LR_VIRTUAL_MASK) << GICH_LR_VIRTUAL_SHIFT;
+    else
+        val |= ((uint64_t)p->irq & GICH_LR_VIRTUAL_MASK) << GICH_LR_VIRTUAL_SHIFT;

Is there supposed to be something different between these two cases? (Or am I missing it?)

Quoted 10 lines
xen/arch/arm/gic-v3.c +2 -0
+
+   if ( p->desc != NULL && !(is_lpi(p->irq)) )
        val |= GICH_LR_HW | (((uint64_t)p->desc->irq & GICH_LR_PHYSICAL_MASK)
                            << GICH_LR_PHYSICAL_SHIFT);
 
xen/arch/arm/gic.c +0 -0
index cfc9c42..091f7e5 100644

xen/arch/arm/gic.c void gic_route_irq_to_xen(struct irq_desc *desc, const cpumask_t *cpu_mask, · line 128

Quoted 6 lines
xen/arch/arm/gic.c +2 -1
@@ -124,18 +124,31 @@ void gic_route_irq_to_xen(struct irq_desc *desc, const cpumask_t *cpu_mask,
                           unsigned int priority)
 {
     ASSERT(priority <= 0xff);     /* Only 8 bits of priority */
-    ASSERT(desc->irq < gic_number_lines());/* Can't route interrupts that don't exist */
+    /* Can't route interrupts that don't exist */
+    ASSERT(desc->irq < gic_number_lines() && is_lpi(desc->irq));

||, surely? Otherwise doesn't this hit for every possible irq?

xen/arch/arm/gic.c void gic_route_irq_to_xen(struct irq_desc *desc, const cpumask_t *cpu_mask, · line 135

Quoted 8 lines
xen/arch/arm/gic.c +2 -1
     ASSERT(test_bit(_IRQ_DISABLED, &desc->status));
     ASSERT(spin_is_locked(&desc->lock));
 
     desc->handler = gic_hw_ops->gic_host_irq_type;
 
-    gic_set_irq_properties(desc, cpu_mask, priority);
+    if ( !is_lpi(desc->irq) )
+        gic_set_irq_properties(desc, cpu_mask, priority);

I think any lack of support for affinity or priority should be pushed down into the lower level drivers, rather than encoding that lack of current support in the pits driver into the generic code.

Quoted 18 lines
xen/arch/arm/gic.c +0 -0
 int gic_route_irq_to_guest(struct domain *d, unsigned int virq,
                            struct irq_desc *desc, unsigned int priority)
 {
xen/arch/arm/irq.c +2 -0
index 9dbdf7d..105ef85 100644
@@ -57,12 +57,22 @@ hw_irq_controller no_irq_type = {
 };
 
 static irq_desc_t irq_desc[NR_IRQS];
+static irq_desc_t irq_desc_lpi[MAX_NR_LPIS];
 static DEFINE_PER_CPU(irq_desc_t[NR_LOCAL_IRQS], local_irq_desc);
 
 irq_desc_t *__irq_to_desc(int irq)
 {
+    struct irq_desc *desc = NULL;
     if (irq < NR_LOCAL_IRQS) return &this_cpu(local_irq_desc)[irq];

xen/arch/arm/irq.c hw_irq_controller no_irq_type = { · line 75

Quoted 10 lines
xen/arch/arm/irq.c +9 -1
-    return &irq_desc[irq-NR_LOCAL_IRQS];
+    else if ( irq >= NR_LOCAL_IRQS && irq < NR_IRQS)
+        return &irq_desc[irq-NR_LOCAL_IRQS];
+    else
+    {
+        if ( is_lpi(irq) )
+            return &irq_desc_lpi[irq - NR_GIC_LPI];
+    }
+
+    return desc;

I'd write this as just a chain of "if (...) return", no need for else or {}s.

Quoted 15 lines
xen/arch/arm/irq.c +15 -0
+void vgic_vcpu_inject_lpi(struct domain *d, unsigned int irq)
+{
+    struct irq_desc *desc;
+    struct pending_irq *p;
+    struct its_device *dev;
+    struct vitt vitt_entry;
+    struct vdevice_table dt_entry;
+    uint32_t devid, col_id;
+    int event;
+
+    desc = irq_to_desc(irq);
+    event =  irq_to_virq(desc);
+
+    dev = get_irq_device(desc);
+    devid = dev->device_id;

xen/arch/arm/irq.c hw_irq_controller no_irq_type = {

Quoted 10 lines
xen/arch/arm/irq.c +10 -0
+    event = irq - dev->lpi_base;
+    if ( event < 0  && event > dev->nr_lpis)
+    {
+        dprintk(XENLOG_WARNING, 
+               "LPI %d received for dev 0x%x is not valid..dropping \n",
+               irq, devid);
+        return;
+    }
+
+    /* validity of device is checheck on vitt entry request */    

Typo

Also trailing whitespace, please clean that up everywhere.

xen/arch/arm/irq.c

Quoted 6 lines
xen/arch/arm/irq.c +6 -0
+    vits_get_vdevice_entry(d, devid, &dt_entry);
+    if ( dt_entry.vitt_ipa != INVALID_PADDR )
+    {
+        dprintk(XENLOG_WARNING,
+                "LPI %d received for dev 0x%x which is disabled..dropping \n",
+                irq, devid);

This drops if the entry is _not_ invalid, is that really right?

Quoted 8 lines
xen/arch/arm/irq.c +8 -0
+        return;
+    }
+
+    if ( vits_get_vitt_entry(d, devid, event, &vitt_entry) )
+    {
+        dprintk(XENLOG_WARNING,
+                "LPI %d received for dev 0x%x which is disabled..dropping \n",
+                irq, devid);

xen/arch/arm/irq.c

Quoted 10 lines
xen/arch/arm/irq.c +10 -0
+        return;
+    }
+    if ( !vitt_entry.valid )
+    {
+        dprintk(XENLOG_WARNING,
+                "LPI %d received for dev 0x%x which is not valid..dropping \n",
+                irq, devid);
+        return;
+    }
+    p = irq_to_pending(d->vcpu[0], vitt_entry.vlpi);

Need to bounds check vitt_entry.vlpi somewhere, since the guest might have messed with it.

xen/arch/arm/irq.c

Quoted 3 lines
xen/arch/arm/irq.c +3 -0
+    col_id = vitt_entry.vcollection;
+
+    ASSERT(col_id < d->max_vcpus);

Likewise a guest which writes vitt_entry.vcollection can now crash the host.

Very great care needs to be taken with both dt_entry and vitt_entry and all of their fields.

xen/arch/arm/irq.c

Quoted 4 lines
xen/arch/arm/irq.c +4 -0
+int vits_get_vitt_entry(struct domain *d, uint32_t devid,
+                        uint32_t event, struct vitt *entry);
+int vits_get_vdevice_entry(struct domain *d, uint32_t devid,
+                           struct vdevice_table *entry);

Please add all prototypes in the same patch as the implementation of the function, not the addition of the first caller.

Ian.

#66 lore raw

Re: [RFC PATCH v3 13/18] xen/arm: ITS: Add irq descriptors for LPIs

Quoted 47 lines · Ian Campbell
Quoted 3 lines · On Mon, 2015-06-22 at 17:31 +0530, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

Add irq descriptors for LPIs and route

This seems to also do interrupt injection for LPIs and more. Please check that your commit messages are accurately describing the contents of the patch. If it turns into a long list of unrelated sounding things then that might suggest the patch should be split up.

Quoted 18 lines
---
 xen/arch/arm/gic-v3.c         |    8 +++-
 xen/arch/arm/gic.c            |   17 +++++++-
 xen/arch/arm/irq.c            |   38 +++++++++++++----
 xen/arch/arm/vgic-v3-its.c    |    9 +++++
 xen/arch/arm/vgic.c           |   90 ++++++++++++++++++++++++++++++++++++++---
 xen/include/asm-arm/domain.h  |    2 +
 xen/include/asm-arm/gic-its.h |    6 +++
 xen/include/asm-arm/gic.h     |    3 ++
 xen/include/asm-arm/vgic.h    |    1 +
 9 files changed, 157 insertions(+), 17 deletions(-)
xen/arch/arm/gic-v3.c +0 -0
index 737646c..793f2f0 100644

xen/arch/arm/gic-v3.c static void gicv3_update_lr(int lr, const struct pending_irq *p, · line 906

Quoted 10 lines
xen/arch/arm/gic-v3.c +4 -2
@@ -899,9 +899,13 @@ static void gicv3_update_lr(int lr, const struct pending_irq *p,
 
     val =  (((uint64_t)state & 0x3) << GICH_LR_STATE_SHIFT) | grp;
     val |= ((uint64_t)p->priority & 0xff) << GICH_LR_PRIORITY_SHIFT;
-    val |= ((uint64_t)p->irq & GICH_LR_VIRTUAL_MASK) << GICH_LR_VIRTUAL_SHIFT;
 
-   if ( p->desc != NULL )
+    if ( is_lpi(p->irq) )
+        val |= ((uint64_t)p->irq & GICH_LR_VIRTUAL_MASK) << GICH_LR_VIRTUAL_SHIFT;
+    else
+        val |= ((uint64_t)p->irq & GICH_LR_VIRTUAL_MASK) << GICH_LR_VIRTUAL_SHIFT;

Is there supposed to be something different between these two cases? (Or am I missing it?)

Quoted 5 lines
xen/arch/arm/gic-v3.c +2 -0
+
+   if ( p->desc != NULL && !(is_lpi(p->irq)) )
        val |= GICH_LR_HW | (((uint64_t)p->desc->irq & GICH_LR_PHYSICAL_MASK)
                            << GICH_LR_PHYSICAL_SHIFT);
 
Quoted 13 lines

xen/arch/arm/gic.c void gic_route_irq_to_xen(struct irq_desc *desc, const cpumask_t *cpu_mask, · line 128

Quoted 11 lines
xen/arch/arm/gic.c +2 -1
index cfc9c42..091f7e5 100644
@@ -124,18 +124,31 @@ void gic_route_irq_to_xen(struct irq_desc *desc, const cpumask_t *cpu_mask,
                           unsigned int priority)
 {
     ASSERT(priority <= 0xff);     /* Only 8 bits of priority */
-    ASSERT(desc->irq < gic_number_lines());/* Can't route interrupts that don't exist */
+    /* Can't route interrupts that don't exist */
+    ASSERT(desc->irq < gic_number_lines() && is_lpi(desc->irq));

||, surely? Otherwise doesn't this hit for every possible irq?

Aside that, the change in the ASSERT is wrong. The goal of the helper gic_number_lines is to return the number of IRQs (i.e PPIs, LPIs, SPIs) present in the hardware. We use in few places to ensure the validity of the IRQ.

Although, this will require some extra care in places where an interrupt is assigned to a domain in order to only allow SPIs.

Regards,

Signature · 1 line
Julien Grall
#67 lore raw

Re: [RFC PATCH v3 13/18] xen/arm: ITS: Add irq descriptors for LPIs

Quoted 62 lines · Julien Grall
Quoted 47 lines · Ian Campbell
Quoted 3 lines · On Mon, 2015-06-22 at 17:31 +0530, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

Add irq descriptors for LPIs and route

This seems to also do interrupt injection for LPIs and more. Please check that your commit messages are accurately describing the contents of the patch. If it turns into a long list of unrelated sounding things then that might suggest the patch should be split up.

Quoted 18 lines
---
 xen/arch/arm/gic-v3.c         |    8 +++-
 xen/arch/arm/gic.c            |   17 +++++++-
 xen/arch/arm/irq.c            |   38 +++++++++++++----
 xen/arch/arm/vgic-v3-its.c    |    9 +++++
 xen/arch/arm/vgic.c           |   90 ++++++++++++++++++++++++++++++++++++++---
 xen/include/asm-arm/domain.h  |    2 +
 xen/include/asm-arm/gic-its.h |    6 +++
 xen/include/asm-arm/gic.h     |    3 ++
 xen/include/asm-arm/vgic.h    |    1 +
 9 files changed, 157 insertions(+), 17 deletions(-)
xen/arch/arm/gic-v3.c +0 -0
index 737646c..793f2f0 100644

xen/arch/arm/gic-v3.c static void gicv3_update_lr(int lr, const struct pending_irq *p, · line 906

Quoted 10 lines
xen/arch/arm/gic-v3.c +4 -2
@@ -899,9 +899,13 @@ static void gicv3_update_lr(int lr, const struct pending_irq *p,
 
     val =  (((uint64_t)state & 0x3) << GICH_LR_STATE_SHIFT) | grp;
     val |= ((uint64_t)p->priority & 0xff) << GICH_LR_PRIORITY_SHIFT;
-    val |= ((uint64_t)p->irq & GICH_LR_VIRTUAL_MASK) << GICH_LR_VIRTUAL_SHIFT;
 
-   if ( p->desc != NULL )
+    if ( is_lpi(p->irq) )
+        val |= ((uint64_t)p->irq & GICH_LR_VIRTUAL_MASK) << GICH_LR_VIRTUAL_SHIFT;
+    else
+        val |= ((uint64_t)p->irq & GICH_LR_VIRTUAL_MASK) << GICH_LR_VIRTUAL_SHIFT;

Is there supposed to be something different between these two cases? (Or am I missing it?)

Quoted 5 lines
xen/arch/arm/gic-v3.c +2 -0
+
+   if ( p->desc != NULL && !(is_lpi(p->irq)) )
        val |= GICH_LR_HW | (((uint64_t)p->desc->irq & GICH_LR_PHYSICAL_MASK)
                            << GICH_LR_PHYSICAL_SHIFT);
 
Quoted 13 lines

xen/arch/arm/gic.c void gic_route_irq_to_xen(struct irq_desc *desc, const cpumask_t *cpu_mask, · line 128

Quoted 11 lines
xen/arch/arm/gic.c +2 -1
index cfc9c42..091f7e5 100644
@@ -124,18 +124,31 @@ void gic_route_irq_to_xen(struct irq_desc *desc, const cpumask_t *cpu_mask,
                           unsigned int priority)
 {
     ASSERT(priority <= 0xff);     /* Only 8 bits of priority */
-    ASSERT(desc->irq < gic_number_lines());/* Can't route interrupts that don't exist */
+    /* Can't route interrupts that don't exist */
+    ASSERT(desc->irq < gic_number_lines() && is_lpi(desc->irq));

||, surely? Otherwise doesn't this hit for every possible irq?

Quoted 7 lines

Aside that, the change in the ASSERT is wrong. The goal of the helper gic_number_lines is to return the number of IRQs (i.e PPIs, LPIs, SPIs) present in the hardware. We use in few places to ensure the validity of the IRQ.

Although, this will require some extra care in places where an interrupt is assigned to a domain in order to only allow SPIs.

Today, gic_number_lines returns number of SPI+PPI supported.
If we want to include number LPIs supported, then we cannot compare against with irq number because of hole in irq numbers from 1024 - 8196.

Regards
Vijay

#68 lore raw

Re: [RFC PATCH v3 13/18] xen/arm: ITS: Add irq descriptors for LPIs

Quoted 68 lines · Vijay Kilari
Quoted 67 lines · Julien Grall
Quoted 47 lines · Ian Campbell
Quoted 3 lines · On Mon, 2015-06-22 at 17:31 +0530, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

Add irq descriptors for LPIs and route

This seems to also do interrupt injection for LPIs and more. Please check that your commit messages are accurately describing the contents of the patch. If it turns into a long list of unrelated sounding things then that might suggest the patch should be split up.

Quoted 18 lines
---
 xen/arch/arm/gic-v3.c         |    8 +++-
 xen/arch/arm/gic.c            |   17 +++++++-
 xen/arch/arm/irq.c            |   38 +++++++++++++----
 xen/arch/arm/vgic-v3-its.c    |    9 +++++
 xen/arch/arm/vgic.c           |   90 ++++++++++++++++++++++++++++++++++++++---
 xen/include/asm-arm/domain.h  |    2 +
 xen/include/asm-arm/gic-its.h |    6 +++
 xen/include/asm-arm/gic.h     |    3 ++
 xen/include/asm-arm/vgic.h    |    1 +
 9 files changed, 157 insertions(+), 17 deletions(-)
xen/arch/arm/gic-v3.c +0 -0
index 737646c..793f2f0 100644

xen/arch/arm/gic-v3.c static void gicv3_update_lr(int lr, const struct pending_irq *p, · line 906

Quoted 10 lines
xen/arch/arm/gic-v3.c +4 -2
@@ -899,9 +899,13 @@ static void gicv3_update_lr(int lr, const struct pending_irq *p,
 
     val =  (((uint64_t)state & 0x3) << GICH_LR_STATE_SHIFT) | grp;
     val |= ((uint64_t)p->priority & 0xff) << GICH_LR_PRIORITY_SHIFT;
-    val |= ((uint64_t)p->irq & GICH_LR_VIRTUAL_MASK) << GICH_LR_VIRTUAL_SHIFT;
 
-   if ( p->desc != NULL )
+    if ( is_lpi(p->irq) )
+        val |= ((uint64_t)p->irq & GICH_LR_VIRTUAL_MASK) << GICH_LR_VIRTUAL_SHIFT;
+    else
+        val |= ((uint64_t)p->irq & GICH_LR_VIRTUAL_MASK) << GICH_LR_VIRTUAL_SHIFT;

Is there supposed to be something different between these two cases? (Or am I missing it?)

Quoted 5 lines
xen/arch/arm/gic-v3.c +2 -0
+
+   if ( p->desc != NULL && !(is_lpi(p->irq)) )
        val |= GICH_LR_HW | (((uint64_t)p->desc->irq & GICH_LR_PHYSICAL_MASK)
                            << GICH_LR_PHYSICAL_SHIFT);
 
Quoted 13 lines

xen/arch/arm/gic.c void gic_route_irq_to_xen(struct irq_desc *desc, const cpumask_t *cpu_mask, · line 128

Quoted 11 lines
xen/arch/arm/gic.c +2 -1
index cfc9c42..091f7e5 100644
@@ -124,18 +124,31 @@ void gic_route_irq_to_xen(struct irq_desc *desc, const cpumask_t *cpu_mask,
                           unsigned int priority)
 {
     ASSERT(priority <= 0xff);     /* Only 8 bits of priority */
-    ASSERT(desc->irq < gic_number_lines());/* Can't route interrupts that don't exist */
+    /* Can't route interrupts that don't exist */
+    ASSERT(desc->irq < gic_number_lines() && is_lpi(desc->irq));

||, surely? Otherwise doesn't this hit for every possible irq?

Aside that, the change in the ASSERT is wrong. The goal of the helper gic_number_lines is to return the number of IRQs (i.e PPIs, LPIs, SPIs) present in the hardware. We use in few places to ensure the validity of the IRQ.

Quoted 7 lines
Quoted 2 lines

Although, this will require some extra care in places where an interrupt is assigned to a domain in order to only allow SPIs.

Today, gic_number_lines returns number of SPI+PPI supported.
If we want to include number LPIs supported, then we cannot compare against with irq number because of hole in irq numbers from 1024 - 8196.

I don't see any problem... All the SPI/PPI reported by the GIC may not be implemented. This ASSERT is here for a sanity check of the IRQ value.

If the caller decides to pass an unused IRQ then it's own problem.

There is not much reason to not extend gic_number_lines. The common GIC code should not care whether the IRQ is a LPI, SPI...

Regards,

Signature · 1 line
Julien Grall
#69 lore raw

Re: [RFC PATCH v3 13/18] xen/arm: ITS: Add irq descriptors for LPIs

Quoted 77 lines · Julien Grall
Quoted 68 lines · Vijay Kilari
Quoted 67 lines · Julien Grall
Quoted 47 lines · Ian Campbell
Quoted 3 lines · On Mon, 2015-06-22 at 17:31 +0530, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

Add irq descriptors for LPIs and route

This seems to also do interrupt injection for LPIs and more. Please check that your commit messages are accurately describing the contents of the patch. If it turns into a long list of unrelated sounding things then that might suggest the patch should be split up.

Quoted 18 lines
---
 xen/arch/arm/gic-v3.c         |    8 +++-
 xen/arch/arm/gic.c            |   17 +++++++-
 xen/arch/arm/irq.c            |   38 +++++++++++++----
 xen/arch/arm/vgic-v3-its.c    |    9 +++++
 xen/arch/arm/vgic.c           |   90 ++++++++++++++++++++++++++++++++++++++---
 xen/include/asm-arm/domain.h  |    2 +
 xen/include/asm-arm/gic-its.h |    6 +++
 xen/include/asm-arm/gic.h     |    3 ++
 xen/include/asm-arm/vgic.h    |    1 +
 9 files changed, 157 insertions(+), 17 deletions(-)
xen/arch/arm/gic-v3.c +0 -0
index 737646c..793f2f0 100644

xen/arch/arm/gic-v3.c static void gicv3_update_lr(int lr, const struct pending_irq *p, · line 906

Quoted 10 lines
xen/arch/arm/gic-v3.c +4 -2
@@ -899,9 +899,13 @@ static void gicv3_update_lr(int lr, const struct pending_irq *p,
 
     val =  (((uint64_t)state & 0x3) << GICH_LR_STATE_SHIFT) | grp;
     val |= ((uint64_t)p->priority & 0xff) << GICH_LR_PRIORITY_SHIFT;
-    val |= ((uint64_t)p->irq & GICH_LR_VIRTUAL_MASK) << GICH_LR_VIRTUAL_SHIFT;
 
-   if ( p->desc != NULL )
+    if ( is_lpi(p->irq) )
+        val |= ((uint64_t)p->irq & GICH_LR_VIRTUAL_MASK) << GICH_LR_VIRTUAL_SHIFT;
+    else
+        val |= ((uint64_t)p->irq & GICH_LR_VIRTUAL_MASK) << GICH_LR_VIRTUAL_SHIFT;

Is there supposed to be something different between these two cases? (Or am I missing it?)

Quoted 5 lines
xen/arch/arm/gic-v3.c +2 -0
+
+   if ( p->desc != NULL && !(is_lpi(p->irq)) )
        val |= GICH_LR_HW | (((uint64_t)p->desc->irq & GICH_LR_PHYSICAL_MASK)
                            << GICH_LR_PHYSICAL_SHIFT);
 
Quoted 13 lines

xen/arch/arm/gic.c void gic_route_irq_to_xen(struct irq_desc *desc, const cpumask_t *cpu_mask, · line 128

Quoted 11 lines
xen/arch/arm/gic.c +2 -1
index cfc9c42..091f7e5 100644
@@ -124,18 +124,31 @@ void gic_route_irq_to_xen(struct irq_desc *desc, const cpumask_t *cpu_mask,
                           unsigned int priority)
 {
     ASSERT(priority <= 0xff);     /* Only 8 bits of priority */
-    ASSERT(desc->irq < gic_number_lines());/* Can't route interrupts that don't exist */
+    /* Can't route interrupts that don't exist */
+    ASSERT(desc->irq < gic_number_lines() && is_lpi(desc->irq));

||, surely? Otherwise doesn't this hit for every possible irq?

Aside that, the change in the ASSERT is wrong. The goal of the helper gic_number_lines is to return the number of IRQs (i.e PPIs, LPIs, SPIs) present in the hardware. We use in few places to ensure the validity of the IRQ.

Quoted 7 lines
Quoted 2 lines

Although, this will require some extra care in places where an interrupt is assigned to a domain in order to only allow SPIs.

Today, gic_number_lines returns number of SPI+PPI supported.
If we want to include number LPIs supported, then we cannot compare against with irq number because of hole in irq numbers from 1024 - 8196.

Quoted 7 lines

I don't see any problem... All the SPI/PPI reported by the GIC may not be implemented. This ASSERT is here for a sanity check of the IRQ value.

If the caller decides to pass an unused IRQ then it's own problem.

There is not much reason to not extend gic_number_lines. The common GIC code should not care whether the IRQ is a LPI, SPI...

I'd rather we refactored these checks into some sort of is_valid_irq() helper which could check vs. nr_lines (which remains the GICD_TYPER.ITLinesNumber based thing) and the number of LPIs separately.

Combining the two risks considering interrupt in the 1025..8191 range as valid and open coding anything more complex than the existing single < check in all the relevant places isn't going to scale.

Ian.

#70 lore raw

Re: [RFC PATCH v3 13/18] xen/arm: ITS: Add irq descriptors for LPIs

Quoted 7 lines · Ian Campbell

I'd rather we refactored these checks into some sort of is_valid_irq() helper which could check vs. nr_lines (which remains the GICD_TYPER.ITLinesNumber based thing) and the number of LPIs separately.

Combining the two risks considering interrupt in the 1025..8191 range as valid and open coding anything more complex than the existing single < check in all the relevant places isn't going to scale.

I would be fine with that too. Although, I think we need to add a comment on top of gic_number_lines to clearly specify this is not included LPIs.

Regards,

Signature · 1 line
Julien Grall
#71 lore raw

Re: [RFC PATCH v3 13/18] xen/arm: ITS: Add irq descriptors for LPIs

Quoted 5 lines · Julien Grall
Quoted 4 lines · Ian Campbell

I'd rather we refactored these checks into some sort of is_valid_irq() helper which could check vs. nr_lines (which remains the GICD_TYPER.ITLinesNumber based thing) and the number of LPIs separately.

Quoted 7 lines
Quoted 3 lines

Combining the two risks considering interrupt in the 1025..8191 range as valid and open coding anything more complex than the existing single < check in all the relevant places isn't going to scale.

I would be fine with that too. Although, I think we need to add a comment on top of gic_number_lines to clearly specify this is not included LPIs.

Strictly speaking an LPI being message signalled is not associated with a line, which is why GICD_TYPER.ITLinesNumber also doesn't consider them. But that's not necessarily all that obvious, so a comment would be appropriate, yes.

Ian.

#72 lore raw

RFC PATCH v3 14/18 xen/arm: ITS: Initialize physical ITS

In reply to [email protected]

From: Vijaya Kumar K <[email protected]>

Initialize physical ITS driver from GIC v3 driver
if LPIs are supported by hardware

---
 xen/arch/arm/gic-v3.c         |    7 +++++++
 xen/include/asm-arm/gic-its.h |    2 ++
 2 files changed, 9 insertions(+)
xen/arch/arm/gic-v3.c +7 -0
index 793f2f0..18971ed 100644
@@ -700,6 +700,10 @@ static int __cpuinit gicv3_cpu_init(void)
     if ( gicv3_enable_redist() )
         return -ENODEV;
 
+        /* Give LPIs a spin */
+    if ( gicv3_info.lpi_supported )
+        its_cpu_init();
+
     /* Set priority on PPI and SGI interrupts */
     priority = (GIC_PRI_IPI << 24 | GIC_PRI_IPI << 16 | GIC_PRI_IPI << 8 |
                 GIC_PRI_IPI);
@@ -1327,6 +1331,9 @@ static int __init gicv3_init(void)
     else
         gicv3_info.lpi_supported = 0;
 
+    if ( gicv3_info.lpi_supported )
+        its_init(&gicv3.rdist_data);
+
     gicv3_dist_init();
     res = gicv3_cpu_init();
     gicv3_hyp_init();
xen/include/asm-arm/gic-its.h +2 -0
index f34a207..c674b3f 100644
@@ -274,6 +274,8 @@ static inline uint32_t its_decode_devid(struct domain *d, its_cmd_block *cmd)
     return (cmd->raw_cmd[0] >> 32);  
 }
 
+int its_cpu_init(void);
+int its_init(struct rdist_prop *rdist);
 void its_set_affinity(struct irq_desc *desc, int cpu);
 void lpi_set_config(struct irq_desc *desc, int enable);
 uint8_t vgic_its_get_priority(struct vcpu *v, uint32_t pid);
Signature · 1 line
1.7.9.5
#73 lore raw

RFC PATCH v3 15/18 xen/arm: ITS: Add domain specific ITS initialization

In reply to [email protected]

From: Vijaya Kumar K <[email protected]>

Add Domain and vcpu specific ITS initialization

---
 xen/arch/arm/gic-v3-its.c     |   17 ++++++++++++++++
 xen/arch/arm/setup.c          |    1 +
 xen/arch/arm/vgic-v3-its.c    |   45 +++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c        |    1 +
 xen/include/asm-arm/gic-its.h |    3 +++
 xen/include/asm-arm/vgic.h    |    1 +
 6 files changed, 68 insertions(+)
xen/arch/arm/gic-v3-its.c +17 -0
index 4471669..8aa1ec5 100644
@@ -1234,6 +1234,23 @@ static int its_force_quiescent(void __iomem *base)
     }
 }
 
+void its_domain_init(struct domain *d)
+{
+    struct its_node *its;
+
+    if ( is_hardware_domain(d) )
+    {
+        list_for_each_entry(its, &its_nodes, entry)
+        {
+            /* XXX: Assign only first physical ITS address */
+            d->arch.vits->phys_base = its->phys_base;
+            d->arch.vits->phys_size = its->phys_size;
+            break;
+        }
+    }
+    /* TODO: DomU */
+}
+
 static int its_probe(struct dt_device_node *node)
 {
     paddr_t its_addr, its_size;
xen/arch/arm/setup.c +1 -0
index 06f8e54..0c1081a 100644
@@ -776,6 +776,7 @@ void __init start_xen(unsigned long boot_phys_offset,
     init_xen_time();
 
     gic_init();
+    vgic_its_init();
 
     p2m_vmid_allocator_init();
 
xen/arch/arm/vgic-v3-its.c +45 -0

51 lines not on this page · raw message

xen/arch/arm/vgic-v3.c +1 -0
index 44922fb..abb1457 100644
@@ -1186,6 +1186,7 @@ static int vgic_v3_domain_init(struct domain *d)
 
     d->arch.vgic.ctlr = VGICD_CTLR_DEFAULT;
 
+    vgic_its_domain_init(d);
     return 0;
 }
 
xen/include/asm-arm/gic-its.h +3 -0
index c674b3f..7e25f5b 100644
@@ -276,6 +276,9 @@ static inline uint32_t its_decode_devid(struct domain *d, its_cmd_block *cmd)
 
 int its_cpu_init(void);
 int its_init(struct rdist_prop *rdist);
+void its_domain_init(struct domain *d);
+int its_lpi_init(u32 id_bits);
+int vgic_its_domain_init(struct domain *d);
 void its_set_affinity(struct irq_desc *desc, int cpu);
 void lpi_set_config(struct irq_desc *desc, int enable);
 uint8_t vgic_its_get_priority(struct vcpu *v, uint32_t pid);
xen/include/asm-arm/vgic.h +1 -0
index f8928ab..4509e9a 100644
@@ -178,6 +178,7 @@ enum gic_sgi_mode;
 #define vgic_num_irqs(d)        ((d)->arch.vgic.nr_spis + 32)
 
 extern int domain_vgic_init(struct domain *d, unsigned int nr_spis);
+extern void vgic_its_init(void);
 extern void domain_vgic_free(struct domain *d);
 extern int vcpu_vgic_init(struct vcpu *v);
 extern struct vcpu *vgic_get_target_vcpu(struct vcpu *v, unsigned int irq);
Signature · 1 line
1.7.9.5
#74 lore raw

Re: [RFC PATCH v3 15/18] xen/arm: ITS: Add domain specific ITS initialization

Quoted 21 lines · On Mon, 2015-06-22 at 17:31 +0530, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

Add Domain and vcpu specific ITS initialization

---
 xen/arch/arm/gic-v3-its.c     |   17 ++++++++++++++++
 xen/arch/arm/setup.c          |    1 +
 xen/arch/arm/vgic-v3-its.c    |   45 +++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c        |    1 +
 xen/include/asm-arm/gic-its.h |    3 +++
 xen/include/asm-arm/vgic.h    |    1 +
 6 files changed, 68 insertions(+)
xen/arch/arm/gic-v3-its.c +0 -0
index 4471669..8aa1ec5 100644
@@ -1234,6 +1234,23 @@ static int its_force_quiescent(void __iomem *base)
     }
 }

xen/arch/arm/gic-v3-its.c static int its_force_quiescent(void __iomem *base) · line 1245

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +9 -0
 
+void its_domain_init(struct domain *d)
+{
+    struct its_node *its;
+
+    if ( is_hardware_domain(d) )
+    {
+        list_for_each_entry(its, &its_nodes, entry)
+        {
+            /* XXX: Assign only first physical ITS address */

I think we settled upon rewriting the msi-parent properties in the dts to all refer to a single vits, in which case putting that vits in the first pits' hole seem ok.

Rather than an XXX (which is a TODO) a comment explaining what is going on would be preferable.

Ian

#75 lore raw

RFC PATCH v3 16/18 xen/arm: ITS: Handle LPI interrupts

In reply to [email protected]

From: Vijaya Kumar K <[email protected]>

Handle and route LPI interrupts

---
 xen/arch/arm/gic.c |    5 +++++
 1 file changed, 5 insertions(+)
xen/arch/arm/gic.c +5 -0
index 091f7e5..802f82f 100644
@@ -665,6 +665,11 @@ void gic_interrupt(struct cpu_user_regs *regs, int is_fiq)
     do  {
         /* Reading IRQ will ACK it */
         irq = gic_hw_ops->read_irq();
+        if ( is_lpi(irq) ) {
+            /* TODO: Enable irqs */
+            do_IRQ(regs, irq, is_fiq);
+            continue;
+        }
 
         if ( likely(irq >= 16 && irq < 1020) )
         {
Signature · 1 line
1.7.9.5
#76 lore raw

Re: [RFC PATCH v3 16/18] xen/arm: ITS: Handle LPI interrupts

Quoted 14 lines · On Mon, 2015-06-22 at 17:31 +0530, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

Handle and route LPI interrupts

---
 xen/arch/arm/gic.c |    5 +++++
 1 file changed, 5 insertions(+)
xen/arch/arm/gic.c +0 -0
index 091f7e5..802f82f 100644

xen/arch/arm/gic.c void gic_interrupt(struct cpu_user_regs *regs, int is_fiq) · line 669

Quoted 5 lines
xen/arch/arm/gic.c +2 -0
@@ -665,6 +665,11 @@ void gic_interrupt(struct cpu_user_regs *regs, int is_fiq)
     do  {
         /* Reading IRQ will ACK it */
         irq = gic_hw_ops->read_irq();
+        if ( is_lpi(irq) ) {
+            /* TODO: Enable irqs */

What was the problem with just doing that?

xen/arch/arm/gic.c void gic_interrupt(struct cpu_user_regs *regs, int is_fiq) · line 671

Quoted 2 lines
xen/arch/arm/gic.c +2 -0
+            do_IRQ(regs, irq, is_fiq);
+            continue;

Either this should be folded with the SPI/PPI handling case below or it should be part of the same if/if else/else chain as the other cases. Ideally the former (i.e. fix the interrupt enabling problem).

Quoted 4 lines
xen/arch/arm/gic.c +1 -0
+        }
 
         if ( likely(irq >= 16 && irq < 1020) )
         {
#77 lore raw

RFC PATCH v3 17/18 xen/arm: ITS: Generate ITS node for Dom0

In reply to [email protected]

From: Vijaya Kumar K <[email protected]>

Parse host dt and generate ITS node for Dom0.
ITS node resides inside GIC node so when GIC node
is encountered look for ITS node.

---
 xen/arch/arm/domain_build.c   |   50 +++++++++++++++++++++++++++++++++++-
 xen/arch/arm/gic-v3-its.c     |   57 +++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h |    2 ++
 3 files changed, 108 insertions(+), 1 deletion(-)
xen/arch/arm/domain_build.c +49 -1
index e9cb8a9..0de5a8b 100644
@@ -20,6 +20,7 @@
 #include <asm/cpufeature.h>
 
 #include <asm/gic.h>
+#include <asm/gic-its.h>
 #include <xen/irq.h>
 #include "kernel.h"
 
@@ -803,6 +804,34 @@ static int make_cpus_node(const struct domain *d, void *fdt,
     return res;
 }
 
+static int make_its_node(const struct domain *d, void *fdt,
+                         const struct dt_device_node *node)
+{
+    int res = 0;
+
+    DPRINT("Create GIC ITS node\n");
+
+    res = its_make_dt_node(d, node, fdt);
+    if ( res )
+        return res;
+
+    /*
+     * The value of the property "phandle" in the property "interrupts"
+     * to know on which interrupt controller the interrupt is wired.
+     */
+    if ( node->phandle )
+    {
+        DPRINT("  Set phandle = 0x%x\n", node->phandle);
+        res = fdt_property_cell(fdt, "phandle", node->phandle);
+        if ( res )
+            return res;
+    }
+
+    res = fdt_end_node(fdt);
+
+    return res;
+}
+
 static int make_gic_node(const struct domain *d, void *fdt,
                          const struct dt_device_node *node)
 {
@@ -1119,7 +1148,13 @@ static int handle_node(struct domain *d, struct kernel_info *kinfo,
         DT_MATCH_TIMER,
         { /* sentinel */ },
     };
+    static const struct dt_device_match gits_matches[] __initconst =
+    {
+        DT_MATCH_GIC_ITS,
+        { /* sentinel */ },
+    };
     struct dt_device_node *child;
+    struct dt_device_node *gic_child;
     int res;
     const char *name;
     const char *path;
@@ -1143,7 +1178,20 @@ static int handle_node(struct domain *d, struct kernel_info *kinfo,
     /* Replace these nodes with our own. Note that the original may be
      * used_by DOMID_XEN so this check comes first. */
     if ( device_get_class(node) == DEVICE_GIC )
-        return make_gic_node(d, kinfo->fdt, node);
+    {
+        if ( !make_gic_node(d, kinfo->fdt, node) )
+        {
+            dt_for_each_child_node(node, gic_child)
+            {
+                if ( gic_child != NULL )
+                {
+                    if ( dt_match_node(gits_matches, gic_child) )
+                        return make_its_node(d, kinfo->fdt, gic_child);
+                }
+            }
+        }
+        return 0;
+    }
     if ( dt_match_node(timer_matches, node) )
         return make_timer_node(d, kinfo->fdt, node);
 
xen/arch/arm/gic-v3-its.c +57 -0

69 lines not on this page · raw message

xen/include/asm-arm/gic-its.h +2 -0
index 7e25f5b..c4c2a07 100644
@@ -274,6 +274,8 @@ static inline uint32_t its_decode_devid(struct domain *d, its_cmd_block *cmd)
     return (cmd->raw_cmd[0] >> 32);  
 }
 
+int its_make_dt_node(const struct domain *d,
+                     const struct dt_device_node *node, void *fdt);
 int its_cpu_init(void);
 int its_init(struct rdist_prop *rdist);
 void its_domain_init(struct domain *d);
Signature · 1 line
1.7.9.5
#78 lore raw

Re: [RFC PATCH v3 17/18] xen/arm: ITS: Generate ITS node for Dom0

Quoted 78 lines · On Mon, 2015-06-22 at 17:31 +0530, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

Parse host dt and generate ITS node for Dom0.
ITS node resides inside GIC node so when GIC node
is encountered look for ITS node.

---
 xen/arch/arm/domain_build.c   |   50 +++++++++++++++++++++++++++++++++++-
 xen/arch/arm/gic-v3-its.c     |   57 +++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h |    2 ++
 3 files changed, 108 insertions(+), 1 deletion(-)
xen/arch/arm/domain_build.c +35 -0
index e9cb8a9..0de5a8b 100644
@@ -20,6 +20,7 @@
 #include <asm/cpufeature.h>
 
 #include <asm/gic.h>
+#include <asm/gic-its.h>
 #include <xen/irq.h>
 #include "kernel.h"
 
@@ -803,6 +804,34 @@ static int make_cpus_node(const struct domain *d, void *fdt,
     return res;
 }
 
+static int make_its_node(const struct domain *d, void *fdt,
+                         const struct dt_device_node *node)
+{
+    int res = 0;
+
+    DPRINT("Create GIC ITS node\n");
+
+    res = its_make_dt_node(d, node, fdt);
+    if ( res )
+        return res;
+
+    /*
+     * The value of the property "phandle" in the property "interrupts"
+     * to know on which interrupt controller the interrupt is wired.
+     */
+    if ( node->phandle )
+    {
+        DPRINT("  Set phandle = 0x%x\n", node->phandle);
+        res = fdt_property_cell(fdt, "phandle", node->phandle);
+        if ( res )
+            return res;
+    }
+
+    res = fdt_end_node(fdt);
+
+    return res;
+}
+
 static int make_gic_node(const struct domain *d, void *fdt,
                          const struct dt_device_node *node)
 {
@@ -1119,7 +1148,13 @@ static int handle_node(struct domain *d, struct kernel_info *kinfo,
         DT_MATCH_TIMER,
         { /* sentinel */ },
     };
+    static const struct dt_device_match gits_matches[] __initconst =
+    {
+        DT_MATCH_GIC_ITS,
+        { /* sentinel */ },
+    };
     struct dt_device_node *child;
+    struct dt_device_node *gic_child;
     int res;
     const char *name;
     const char *path;
@@ -1143,7 +1178,20 @@ static int handle_node(struct domain *d, struct kernel_info *kinfo,
     /* Replace these nodes with our own. Note that the original may be
      * used_by DOMID_XEN so this check comes first. */
     if ( device_get_class(node) == DEVICE_GIC )

xen/arch/arm/domain_build.c static int handle_node(struct domain *d, struct kernel_info *kinfo, · line 1189

Quoted 10 lines
xen/arch/arm/domain_build.c +9 -1
-        return make_gic_node(d, kinfo->fdt, node);
+    {
+        if ( !make_gic_node(d, kinfo->fdt, node) )
+        {
+            dt_for_each_child_node(node, gic_child)
+            {
+                if ( gic_child != NULL )
+                {
+                    if ( dt_match_node(gits_matches, gic_child) )
+                        return make_its_node(d, kinfo->fdt, gic_child);

This will create multiple ITS nodes, we only want one and we need the msi-parent properties everywhere else to be changed to point to it.

Quoted 28 lines
xen/arch/arm/domain_build.c +5 -0
+                }
+            }
+        }
+        return 0;
+    }
     if ( dt_match_node(timer_matches, node) )
         return make_timer_node(d, kinfo->fdt, node);
 
xen/arch/arm/gic-v3-its.c +5 -0
index 8aa1ec5..fc853d4 100644
@@ -27,6 +27,8 @@
 #include <xen/sched.h>
 #include <xen/errno.h>
 #include <xen/delay.h>
+#include <xen/device_tree.h>
+#include <xen/libfdt/libfdt.h>
 #include <xen/list.h>
 #include <xen/sizes.h>
 #include <xen/vmap.h>
@@ -1205,6 +1207,61 @@ static void its_cpu_init_collection(void)
     spin_unlock(&its_lock);
 }
 
+int its_make_dt_node(const struct domain *d,
+                     const struct dt_device_node *node, void *fdt)
+{

xen/arch/arm/gic-v3-its.c static void its_cpu_init_collection(void) · line 1222

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+    struct its_node *its;
+    const struct dt_device_node *gic;
+    const void *compatible = NULL;
+    u32 len;
+    __be32 *new_cells, *tmp;
+    int res = 0;
+
+    /* Will pass only first ITS node info */
+    /* TODO: Handle multi node */
+    its = list_first_entry(&its_nodes, struct its_node, entry);

I think this should be done at the top level by not walking all children and by blacklisting all ITS nodes to be replaced by our own.

It's also not clear why list_first_entry should be NULL for all but the first, but nevermind.

Quoted 58 lines
xen/arch/arm/gic-v3-its.c +42 -0
+    if ( !its )
+    {
+        dprintk(XENLOG_ERR, "ITS node not found\n");
+        return -FDT_ERR_XEN(ENOENT);
+    }
+
+    gic = its->dt_node;
+
+    compatible = dt_get_property(gic, "compatible", &len);
+    if ( !compatible )
+    {
+        dprintk(XENLOG_ERR, "Can't find compatible property for the its node\n");
+        return -FDT_ERR_XEN(ENOENT);
+    }
+
+    res = fdt_begin_node(fdt, "gic-its");
+    if ( res )
+        return res;
+
+    res = fdt_property(fdt, "compatible", compatible, len);
+    if ( res )
+        return res;
+
+    res = fdt_property(fdt, "msi-controller", NULL, 0);
+    if ( res )
+        return res;
+
+    len = dt_cells_to_size(dt_n_addr_cells(node) + dt_n_size_cells(node));
+
+    new_cells = xzalloc_bytes(len);
+    if ( new_cells == NULL )
+        return -FDT_ERR_XEN(ENOMEM);
+    tmp = new_cells;
+
+    dt_set_range(&tmp, node, its->phys_base, its->phys_size);
+
+    res = fdt_property(fdt, "reg", new_cells, len);
+    xfree(new_cells);
+
+    return res;
+}
+
 static int its_force_quiescent(void __iomem *base)
 {
     u32 count = 1000000;   /* 1s */
xen/include/asm-arm/gic-its.h +2 -0
index 7e25f5b..c4c2a07 100644
@@ -274,6 +274,8 @@ static inline uint32_t its_decode_devid(struct domain *d, its_cmd_block *cmd)
     return (cmd->raw_cmd[0] >> 32);  
 }
 
+int its_make_dt_node(const struct domain *d,
+                     const struct dt_device_node *node, void *fdt);
 int its_cpu_init(void);
 int its_init(struct rdist_prop *rdist);
 void its_domain_init(struct domain *d);
#79 lore raw

Re: [RFC PATCH v3 17/18] xen/arm: ITS: Generate ITS node for Dom0

Quoted 19 lines · Ian Campbell
Quoted 4 lines · On Mon, 2015-06-22 at 17:31 +0530, [email protected] wrote:
xen/arch/arm/gic-v3-its.c +3 -0
+int its_make_dt_node(const struct domain *d,
+                     const struct dt_device_node *node, void *fdt)
+{

xen/arch/arm/gic-v3-its.c static void its_cpu_init_collection(void) · line 1222

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+    struct its_node *its;
+    const struct dt_device_node *gic;
+    const void *compatible = NULL;
+    u32 len;
+    __be32 *new_cells, *tmp;
+    int res = 0;
+
+    /* Will pass only first ITS node info */
+    /* TODO: Handle multi node */
+    its = list_first_entry(&its_nodes, struct its_node, entry);

I think this should be done at the top level by not walking all children and by blacklisting all ITS nodes to be replaced by our own.

I have added TODO for multinode.
For now, I plan to support only single ITS node. I don't have platform support right now to test multinode

#80 lore raw

Re: [RFC PATCH v3 17/18] xen/arm: ITS: Generate ITS node for Dom0

Hi,

Quoted 21 lines · Vijay Kilari
Quoted 19 lines · Ian Campbell
Quoted 4 lines · On Mon, 2015-06-22 at 17:31 +0530, [email protected] wrote:
xen/arch/arm/gic-v3-its.c +3 -0
+int its_make_dt_node(const struct domain *d,
+                     const struct dt_device_node *node, void *fdt)
+{

xen/arch/arm/gic-v3-its.c static void its_cpu_init_collection(void) · line 1222

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+    struct its_node *its;
+    const struct dt_device_node *gic;
+    const void *compatible = NULL;
+    u32 len;
+    __be32 *new_cells, *tmp;
+    int res = 0;
+
+    /* Will pass only first ITS node info */
+    /* TODO: Handle multi node */
+    its = list_first_entry(&its_nodes, struct its_node, entry);

I think this should be done at the top level by not walking all children and by blacklisting all ITS nodes to be replaced by our own.

Quoted 3 lines

I have added TODO for multinode.
For now, I plan to support only single ITS node. I don't have platform support right now to test multinode

I'm not sure to understand what you want to support multi-node here...

This function is used to create the ITS node for DOM0. From the design document, guest domain only sees a single node.

Blacklisted ITS node in the top level function is just one line (you have to give the compatible string)>

Regards,

Signature · 1 line
Julien Grall
#81 lore raw

Re: [RFC PATCH v3 17/18] xen/arm: ITS: Generate ITS node for Dom0

Quoted 36 lines · Julien Grall

Hi,

Quoted 27 lines · Vijay Kilari
Quoted 14 lines · Ian Campbell
Quoted 10 lines · On Mon, 2015-06-22 at 17:31 +0530, [email protected] wrote:
xen/arch/arm/gic-v3-its.c +9 -0
+int its_make_dt_node(const struct domain *d,
+                     const struct dt_device_node *node, void *fdt)
+{
+    struct its_node *its;
+    const struct dt_device_node *gic;
+    const void *compatible = NULL;
+    u32 len;
+    __be32 *new_cells, *tmp;
+    int res = 0;
Quoted 8 lines

xen/arch/arm/gic-v3-its.c static void its_cpu_init_collection(void) · line 1222

Quoted 4 lines
xen/arch/arm/gic-v3-its.c +4 -0
+
+    /* Will pass only first ITS node info */
+    /* TODO: Handle multi node */
+    its = list_first_entry(&its_nodes, struct its_node, entry);

I think this should be done at the top level by not walking all children and by blacklisting all ITS nodes to be replaced by our own.

Quoted 3 lines

I have added TODO for multinode.
For now, I plan to support only single ITS node. I don't have platform support right now to test multinode

Quoted 7 lines

I'm not sure to understand what you want to support multi-node here...

This function is used to create the ITS node for DOM0. From the design document, guest domain only sees a single node.

Blacklisted ITS node in the top level function is just one line (you have to give the compatible string)>

AIUI, blacklisting ITS node mean, skipping the node getting generated for dom0 by adding compatible string to skip_matches[] list in handle_node() function of domain_build.c right?

If so, ITS node resides within GIC node, I don't think adding to skip_matches[] list skips it.

So In the new patch, within GIC node I have checked for child nodes which matches to ITS compatible
string and generated only one node for Dom0 as below.

    if ( device_get_class(node) == DEVICE_GIC )
    {
        if ( !make_gic_node(d, kinfo->fdt, node) )
        {
            res = 0;
            dt_for_each_child_node(node, gic_child)
            {
                if ( gic_child != NULL )
                {
                    if ( dt_match_node(gits_matches, gic_child) )
                    {
                        res = make_its_node(d, kinfo->fdt, gic_child);
                        break;
                    }
                }
            }
            return res;
        }
        return 0;
    }

However if DT has more than one ITS node, then all references in the DT (PCI nodes)
should be changed to pointed to single (generated) ITS node. This is what I meant
for TODO. May be TODO is placed wrongly in its driver

Regards
Vijay

#82 lore raw

Re: [RFC PATCH v3 17/18] xen/arm: ITS: Generate ITS node for Dom0

Quoted 42 lines · Vijay Kilari
Quoted 41 lines · Julien Grall

Hi,

Quoted 27 lines · Vijay Kilari
Quoted 14 lines · Ian Campbell
Quoted 10 lines · On Mon, 2015-06-22 at 17:31 +0530, [email protected] wrote:
xen/arch/arm/gic-v3-its.c +9 -0
+int its_make_dt_node(const struct domain *d,
+                     const struct dt_device_node *node, void *fdt)
+{
+    struct its_node *its;
+    const struct dt_device_node *gic;
+    const void *compatible = NULL;
+    u32 len;
+    __be32 *new_cells, *tmp;
+    int res = 0;
Quoted 8 lines

xen/arch/arm/gic-v3-its.c static void its_cpu_init_collection(void) · line 1222

Quoted 4 lines
xen/arch/arm/gic-v3-its.c +4 -0
+
+    /* Will pass only first ITS node info */
+    /* TODO: Handle multi node */
+    its = list_first_entry(&its_nodes, struct its_node, entry);

I think this should be done at the top level by not walking all children and by blacklisting all ITS nodes to be replaced by our own.

Quoted 3 lines

I have added TODO for multinode.
For now, I plan to support only single ITS node. I don't have platform support right now to test multinode

I'm not sure to understand what you want to support multi-node here...

This function is used to create the ITS node for DOM0. From the design document, guest domain only sees a single node.

Quoted 6 lines
Quoted 2 lines

Blacklisted ITS node in the top level function is just one line (you have to give the compatible string)>

AIUI, blacklisting ITS node mean, skipping the node getting generated for dom0 by adding compatible string to skip_matches[] list in handle_node() function of domain_build.c right?

Yes.

Quoted 2 lines

If so, ITS node resides within GIC node, I don't think adding to skip_matches[] list skips it.

In the case of the GIC node, we skip all the childs so ITS node are already blacklisted by default.

You can create the ITS node in the hwdom_dt_node callback. I don't see why you need to browse all the ITS nodes in the function handle_device, which is common to every one.

Quoted 5 lines

However if DT has more than one ITS node, then all references in the DT (PCI nodes)
should be changed to pointed to single (generated) ITS node. This is what I meant
for TODO. May be TODO is placed wrongly in its driver

The changes in only a couple of lines in write_properties. If you don't plan to handle multiple ITS in your series, you need to add a big warning/panic rather letting Xen booting on it fine.

Regards,

Signature · 1 line
Julien Grall
#83 lore raw

RFC PATCH v3 18/18 xen/arm: ITS: Map ITS translation space

In reply to [email protected]

From: Vijaya Kumar K <[email protected]>

ITS translation space contains GITS_TRANSLATOR register which is written by device to raise LPI. This space needs to mapped to every domain address space for all physical ITS available, so that device can access GITS_TRANSLATOR register using SMMU.

---
 xen/arch/arm/vgic-v3-its.c |   32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)
xen/arch/arm/vgic-v3-its.c +31 -1
index 00e210f..7cf029a 100644
@@ -1174,6 +1174,36 @@ static const struct mmio_handler_ops vgic_gits_mmio_handler = {
     .write_handler = vgic_v3_gits_mmio_write,
 };
 
+/*
+ * Map the 64K ITS translation space in guest.
+ * This is required purely for device smmu writes.
+*/
+
+//static int vgic_map_translation_space(uint32_t nr_its, struct domain *d)
+static int vgic_map_translation_space(struct domain *d)
+{
+    uint64_t addr, size;
+    int ret;
+
+    addr = d->arch.vits->phys_base + SZ_64K;
+    size = SZ_64K;
+
+    ret = map_mmio_regions(d,
+                           paddr_to_pfn(addr & PAGE_MASK),
+                           DIV_ROUND_UP(size, PAGE_SIZE),
+                           paddr_to_pfn(addr & PAGE_MASK));
+
+     if ( ret )
+     {
+          dprintk(XENLOG_G_ERR, "vITS: Unable to map to dom%d access to"
+                  " 0x%"PRIx64" - 0x%"PRIx64"\n",
+                  d->domain_id,
+                  addr & PAGE_MASK, PAGE_ALIGN(addr + size) - 1);
+     }
+
+    return ret;
+}
+
 int vgic_its_domain_init(struct domain *d)
 {
     int i;
@@ -1210,7 +1240,7 @@ int vgic_its_domain_init(struct domain *d)
                           d->arch.vits->phys_base,
                           SZ_64K);
 
-    return 0;
+    return vgic_map_translation_space(d);
 }
 
 void vgic_its_init(void)
Signature · 1 line
1.7.9.5
#84 lore raw
In reply to [email protected]

Hi Vijay,

Quoted 11 lines · On 22/06/15 13:01, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

This is based on DraftF version
http://xenbits.xen.org/people/ianc/vits/draftF.pdf

Following major features are supported
- GICv3 ITS support for arm64 platform
- Only Dom0 is supported

Basic boot is tested with single ITS node with hacks in pci driver.

What about a proper support without hack?

Quoted 2 lines

All Comments from V2 version are not
fixed as this series is based on new design.

I agree that any comments on code that is not present anymore should be skipped. But all the others should have been fixed and it's not the case.

The time I will spend to find the same errors and write the same comments could have been spend more usefully to review the new design...

Please help us to speed up the review and make possible to merge ITS support in Xen 4.6.

Regards,

Signature · 1 line
Julien Grall
#85 lore raw
Quoted 8 lines · Julien Grall

Hi Vijay,

Quoted 5 lines · On 22/06/15 13:01, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

This is based on DraftF version
http://xenbits.xen.org/people/ianc/vits/draftF.pdf

Quoted 8 lines
Quoted 6 lines

Following major features are supported
- GICv3 ITS support for arm64 platform
- Only Dom0 is supported

Basic boot is tested with single ITS node with hacks in pci driver.

What about a proper support without hack?

I asked Vijay to send an RFC without waiting for that.

Ian.

#86 lore raw
In reply to [email protected]
Quoted 4 lines · On Mon, 2015-06-22 at 17:31 +0530, [email protected] wrote:

From: Vijaya Kumar K <[email protected]>

This is based on DraftF version
http://xenbits.xen.org/people/ianc/vits/draftF.pdf

I had some local edits due to comments made on IRC and in response to questions you asked while implementing etc. It's nothing major and I don't think it will have an impact on the implementation but rather than have it sitting on my HDD I've stuck it at
http://xenbits.xen.org/people/ianc/vits/draftG.pdf and below.

Ian.

% Xen on ARM vITS Handling
% Ian Campbell <[email protected]>
% Draft G

# Changelog

## Since Draft F

* Fix wording in INV and INVALL cases.
* Drop attempt at preemptible GITS_CWRITER handling.
* Clarify physical vs virtual collections and physical vs virtual LPIs in some places.

## Since Draft E

* Discussion of struct pending_irq
* Fix handling of enable/disable, requiring switching back to trapping the virtual cfg table again. get_vlpi_cfg is no longer needed.
* Fix p2m_lookup to also use get_page_from_gfn.

## Since Draft D

* Fixed assumptions about vLPI->pLPI mapping, which is not possible. This lead to changes to the model for enabling and disabling pLPI and vLPI and the handling of the virtual LPI configuration table, resolving _Unresolved Issue 1_.
* Made the pLPI and vLPI interrupt priorities explicit.
* Attempted to clarify the trust issues regarding in-guest data structures.
* Mandate a particular cacheability for tables in guest memory.

## Since Draft C

* _Major_ rework, in an attempt to simplify everything into something more likely to be achievable for 4.6.
* Made some simplifying assumptions.
* Reduced the scope of some support.
* Command emulation is now mostly trivial.
* Expanded detail on host setup, allowing other assumptions to be made during emulation.
* Many other things lost in the noise of the above.

## Since Draft B

* Details of command translation (thanks to Julien and Vijay)
* Added background on LPI Translation and Pending tables
* Added background on Collections
* Settled on N:N scheme for vITS:pat's mapping.
* Rejigged section nesting a bit.
* Since we now thing translation should be cheap, settle on translation at scheduling time.
* Lazy INVALL and SYNC

## Since Draft A

* Added discussion of when/where command translation occurs.
* Contention on scheduler lock, suggestion to use SOFTIRQ.
* Handling of domain shutdown.
* More detailed discussion of multiple vs single vits pros/cons.

# Introduction

ARM systems containing a GIC version 3 or later may contain one or more ITS logical blocks. An ITS is used to route Message Signalled interrupts from devices into an LPI injection on the processor.

The following summarises the ITS hardware design and serves as a set of assumptions for the vITS software design. For full details of the ITS see the "GIC Architecture Specification".

## Locality-specific Peripheral Interrupts (LPI)

This is a new class of message signalled interrupts introduced in GICv3. They occupy the interrupt ID space from 8192..(2^32)-1.

The number of LPIs support by an ITS is exposed via GITS_TYPER.IDbits (as number of bits - 1), it may be up to 2^32. _Note_: This field also contains the number of Event IDs supported by the ITS.

### LPI Configuration Table

Each LPI has an associated configuration byte in the LPI Configuration Table (managed via the GIC Redistributor and placed at GICR_PROPBASER or GICR_VPROPBASER). This byte configures:

* The LPI's priority;
* Whether the LPI is enabled or disabled.

Software updates the Configuration Table directly but must then issue an invalidate command (per-device INV ITS command, global INVALL ITS command or write GICR_INVLPIR) for the affect to be guaranteed to become visible (possibly requiring an ITS SYNC command to ensure completion of the INV or INVALL). Note that it is valid for an implementation to reread the configuration table at any time (IOW it is _not_ guaranteed that a change to the LPI Configuration Table won't be visible until an invalidate is issued).

### LPI Pending Table

Each LPI also has an associated bit in the LPI Pending Table (managed by the GIC redistributor). This bit signals whether the LPI is pending or not.

This region may contain out of date information and the mechanism to synchronise is IMPLEMENTATION DEFINED.

## Interrupt Translation Service (ITS)

### Device Identifiers

Each device using the ITS is associated with a unique "Device Identifier".

The device IDs are properties of the implementation and are typically described via system firmware, e.g. the ACPI IORT table or via device tree.

The number of device ids in a system depends on the implementation and can be discovered via GITS_TYPER.Devbits. This field allows an ITS to have up to 2^32 devices.

### Events

Each device can generate "Events" (called ID in the spec) these correspond to possible interrupt sources in the device (e.g. MSI offset).

The maximum number of interrupt sources is device specific. It is usually discovered either from firmware tables (e.g. DT or ACPI) or from bus specific mechanisms (e.g. PCI config space).

The maximum number of events ids support by an ITS is exposed via GITS_TYPER.IDbits (as number of bits - 1), it may be up to 2^32. _Note_: This field also contains the number of LPIs supported by the ITS.

### Interrupt Collections

Each interrupt is a member of an "Interrupt Collection". This allows software to manage large numbers of physical interrupts with a small number of commands rather than issuing one command per interrupt.

On a system with N processors, the ITS must provide at least N+1 collections.

An ITS may support some number of internal collections (indicated by GITS_TYPER.HCC) and external ones which require memory provisioned by the Operating System via a GITS_BASERn register.

### Target Addresses

The Target Address correspond to a specific GIC re-distributor. The format of this field depends on the value of the GITS_TYPER.PTA bit:

* 1: the base address of the re-distributor target is used
* 0: a unique processor number is used. The mapping between the processor affinity value (MPIDR) and the processor number is discoverable via GICR_TYPER.ProcessorNumber.

This value is up to the ITS implementer (GITS_TYPER is a read-only register).

### Device Table

A Device Table is configured in each ITS which maps incoming device identifiers into an ITS Interrupt Translation Table.

### Interrupt Translation Table (ITT) and Collection Table

An Event generated by a Device is translated into an LPI via a per-Device Interrupt Translation Table. The structure of this table is described in GIC Spec 4.9.12.

The ITS translation table maps the device id of the originating device into a physical interrupt (LPI) and an Interrupt Collection.

The Collection is in turn looked up in the Collection Table to produce a Target Address, indicating a redistributor (AKA CPU) to which the LPI is delivered.

### OS Provisioned Memory Regions

The ITS hardware design provides mechanisms for an ITS to be provided with various blocks of memory by the OS for ITS internal use, this include the per-device ITT (established with MAPD) and memory regions for Device Tables, Virtual Processors and Interrupt Collections. Up to 8 such regions can be requested by the ITS and provisioned by the OS via the GITS_BASERn registers.

### ITS Configuration

The ITS is configured and managed, including establishing and configuring the Translation Tables and Collection Table, via an in memory ring shared between the CPU and the ITS controller. The ring is managed via the GITS_CBASER register and indexed by GITS_CWRITER and GITS_CREADR registers.

A processor adds commands to the shared ring and then updates GITS_CWRITER to make them visible to the ITS controller.

The ITS controller processes commands from the ring and then updates GITS_CREADR to indicate the the processor that the command has been processed.

Commands are processed sequentially.

Commands sent on the ring include operational commands:

* Routing interrupts to processors;
* Generating interrupts;
* Clearing the pending state of interrupts;
* Synchronising the command queue

and maintenance commands:

* Map device/collection/processor;
* Map virtual interrupt;
* Clean interrupts;
* Discard interrupts;

The field GITS_CBASER.Size encodes the number of 4KB pages minus 0 consisting of the command queue. This field is 8 bits which means the maximum size is 2^8 * 4KB = 1MB. Given that each command is 32 bytes, there is a maximum of 32768 commands in the queue.

The ITS provides no specific completion notification mechanism. Completion is monitored by a combination of a SYNC command and either polling GITS_CREADR or notification via an interrupt generated via the INT command.

Note that the interrupt generation via INT requires an originating device ID to be supplied (which is then translated via the ITS into an LPI). No specific device ID is defined for this purpose and so the OS software is expected to fabricate one.

Possible ways of inventing such a device ID are:

* Enumerate all device ids in the system and pick another one;
* Use a PCI BDF associated with a non-existent device function (such as an unused one relating to the PCI root-bridge) and translate that (via firmware tables) into a suitable device id;
* ???

# LPI Handling in Xen

## IRQ descriptors

Currently all SGI/PPI/SPI interrupts are covered by a single static array of struct irq_desc with ~1024 entries (the maximum interrupt number in that set of interrupt types).

The addition of LPIs in GICv3 means that the largest potential interrupt specifier is much larger.

Therefore a second dynamically allocated array will be added to cover the range 8192..nr_lpis. The irq_to_desc function will determine which array to use (static 0..1024 or dynamic 8192..end lpi desc array) based on the input irq number. Two arrays are used to avoid a wasteful allocation covering the unused/unusable) 1024..8191 range.

## Virtual LPI interrupt injection

A physical interrupt which is routed to a guest vCPU has the _IRQ_GUEST flag set in the irq_desc status mask. Such interrupts have an associated instance of struct irq_guest which contains the target struct domain pointer and virtual interrupt number.

In Xen a virtual interrupt (either arising from a physical interrupt or completely virtual) is ultimately injected to a VCPU using the vgic_vcpu_inject_irq function, or vgic_vcpu_inject_lpi.

This mechanism will likely need updating to handle the injection of virtual LPIs. In particular rather than GICD_ITARGERRn or GICD_IROUTERn routing of LPIs is performed via the ITS collections mechanism. This is discussed below (In _vITS_:_Virtual LPI injection_).

# Scope

The ITS is rather complicated, especially when combined with virtualisation. To simplify things we initially omit the following functionality:

- Interrupt -> vCPU -> pCPU affinity. The management of physical vs virtual Collections is a feature of GICv4, thus is omitted in this design for GICv3. Physical interrupts which occur on a pCPU where the target vCPU is not already resident will be forwarded (via IPI) to the correct pCPU for injection via the existing vgic_vcpu_inject_irq mechanism (extended to handle LPI injection correctly).
- Clearing of the pending state of an LPI under various circumstances (MOVI, DISCARD, CLEAR commands) is not done. This will result in guests seeing some perhaps spurious interrupts.
- vITS functionality will only be available on 64-bit ARM hosts, avoiding the need to worry about fast access to guest owned data structures (64-bit uses a direct map). (NB: 32-bit guests on 64-bit hosts can be considered to have access)

# pITS

## Assumptions

It is assumed that GITS_TYPER.IDbits is large enough that there are sufficient LPIs available to cover the sum of the number of possible events generated by each device in the system (that is the sum of the actual events for each bit of hardware, rather than the notional per-device maximum from GITS_TYPER.Idbits).

This assumption avoids the need to do memory allocations and interrupt routing at run time, e.g. during command processing by allowing us to setup everything up front.

## Driver

The physical driver will provide functions for enabling, disabling routing etc a specified interrupt, via the usual Xen APIs for doing such things.

This will likely involve interacting with the physical ITS command queue etc. In this document such interactions are considered internal to the driver (i.e. we care that the API to enable an interrupt exists, not how it is implemented).

The physical ITS will be provisioned with whatever tables it requests via its GITS_BASERn registers.

## Collections

The pITS will be configured at start of day with 1 Collection mapped to each physical processor, using the MAPC command on the physical ITS.

## Per Device Information

Each physical device in the system which can be used together with an ITS (whether using passthrough or not) will have associated with it a data structure:

    struct its_device {
        struct pits *pits;
        uintNN_t phys_device_id;
        uintNN_t virt_device_id;
        unsigned int *events;
        unsigned int nr_events;
        struct page_info *pitt;
        unsigned int nr_pitt_pages;
        /* Other fields relating to pITS maintenance but unrelated to vITS */
    };

Where:

- pits: Pointer to the associated physical ITS.
- phys_device_id: The physical device ID of the physical device
- virt_device_id: The virtual device ID if the device is accessible to a domain
- events: An array mapping a per-device event number into a physical LPI.
- nr_events: The number of events which this device is able to generate.
- pitt, nr_pitt_pages: Records allocation of pages for physical ITT (not directly accessible).

During its lifetime this structure may be referenced by several different mappings (e.g. physical and virtual device id maps, virtual collection device id).

## Device Discovery/Registration and Configuration

Per device information will be discovered based on firmware tables (DT or ACPI) and information provided by dom0 (e.g. reading associated PCI cfg space, registration via PHYSDEVOP_pci_device_add or new custom hypercalls).

This information shall include at least:

- The Device ID of the device.
- The maximum number of Events which the device is capable of generating.

When a device is discovered/registered (i.e. when all necessary information is available) then:

- struct its_device and the embedded events array will be allocated (the latter with nr_events elements).
- The struct its_device will be inserted into a mapping (possibly an R-B tree) from its physical Device ID to the struct its.
- nr_events physical LPIs will be allocated and recorded in the events array.
- An ITT table will be allocated for the device and the appropriate MAPD command will be issued to the physical ITS. The location will be recorded in struct its_device.pitt.
- Each Event which the device may generate will be mapped to the corresponding LPI in the events array and a collection, by issuing a series of MAPVI commands. Events will be assigned to physical collections in a round-robin fashion.

This setup must occur for a given device before any ITS interrupts may be configured for the device and certainly before a device is passed through to a guest. This implies that dom0 cannot use MSIs on a PCI device before having called PHYSDEVOP_pci_device_add.

# Device Assignment

Each domain will have an associated mapping from virtual device ids into a data structure describing the physical device, including a reference to the relevant struct its_device.

The number of possible device IDs may be large so a simple array or list is likely unsuitable. A tree (e.g. Red-Black may be a suitable data structure. Currently we do not need to perform lookups in this tree on any hot paths.

_Note_: In the context of virtualised device ids (especially for domU) it may be possible to arrange for the upper bound on the number of device IDs to be lower allowing a more efficient data structure to be used. This is left for a future improvement.

When a device is assigned to a domain (including to domain 0) the mapping for the new virtual device ID will be entered into the tree.

During assignment all LPIs associated with the device will be routed to the guest (i.e. route_irq_to_guest will be called for each LPI in the struct its_device.events array) and the pLPI will be enabled in the physical LPI configuration table with a priority of GIC_PRI_IRQ (not any priority from the guest).

# vITS

A guest domain which is allowed to use ITS functionality (i.e. has been assigned pass-through devices which can generate MSIs) will be presented with a virtualised ITS.

Accesses to the vITS registers will trap to Xen and be emulated and a virtualised Command Queue will be provided.

Commands entered onto the virtual Command Queue will be translated into physical commands, as described later in this document.

There are other aspects to virtualising the ITS (LPI collection management, assignment of LPI ranges to guests, device management). However these are only considered here to the extent needed for describing the vITS emulation.

## Xen interaction with guest OS provisioned vITS memory

Memory which the guest provisions to the vITS (ITT via MAPD or other tables via GITS_BASERn) needs careful handling in Xen.

### Trust

Since Xen cannot trust data in data structures contained in such memory if a guest can trample over it at will. Therefore Xen either must take great care when accessing data structures stored in such memory to validate the contents e.g. not trust that values are within the required limits or it must take steps to restrict guest access to the memory when it is provisioned. Since the data structures are simple and most accessors need to do bounds check anyway it is considered sufficient to simply do the necessary checks on access.

**Any information read memory which has been provisioned by the guest
OS should not be trusted and must be carefully checked (e.g. ranges etc) before use.**

### Mapping

Most data structures stored in this shared memory are accessed on the hot interrupt injection path and must therefore be quickly accessible from within Xen. Since we have restricted vits support to 64-bit hosts only map_domain_page is fast enough to be used on the fly and therefore we do not need to be concerned about unbounded amounts of permanently mapped memory consumed by each MAPD command.

Although map_domain_page is fast, p2m_lookup (translation from IPA to PA) is not necessarily so and is used by get_page_from_gfn. For now we accept this, as a future extension a sparse mapping of the guest device table in vmap space could be considered, with limits on the total amount of vmap space which we allow each domain to consume.

The GITS_BASERn registers allow for the guest to specify cache attributes for the memory. For now we require that these have the same attributes as hypercall arguments in general (see public/arch-arm.h)

In addition while GITS_BASERn allows the Cacheability to be specified as Device-nGnRnE we require that the tables provided be in normal guest RAM (not MMIO, not granted memory etc), that is it must have type p2m_ram_rw.

## vITS properties

The vITS implementation shall have:

- GITS_TYPER.HCC == nr_vcpus + 1.
- GITS_TYPER.PTA == 0. Target addresses are linear processor numbers.
- GITS_TYPER.Devbits == See below.
- GITS_TYPER.IDbits == See below.
- GITS_TYPER.ITT Entry Size == 7, meaning 8 bytes, which is the size of struct vitt (defined below).

GITS_TYPER.Devbits and GITS_TYPER.Idbits will need to be chosen to reflect the host and guest configurations (number of LPIs, maximum device ID etc).

Other fields (not mentioned here) will be set to some sensible (or mandated) value.

The GITS_BASER0 will be setup to request sufficient memory for a device table consisting of entries of:

    struct vdevice_table {
        uint64_t vitt_ipa;
        uint32_t vitt_size;
        uint32_t padding;
    };
    BUILD_BUG_ON(sizeof(struct vdevice_table) != 16);

On write to GITS_BASER0 the relevant details of the Device Table (IPA, size, cache attributes to use when mapping) will be recorded in struct domain.

All other GITS_BASERn.Valid == 0.

## vITS to pITS mapping

A physical system may have multiple physical ITSs.

With the simplified vits command model presented here only a single vits is required.

In the future a more complex arrangement may be desired. Since the choice of model is internal to the hypervisor/tools and is communicated to the guest via firmware tables we are not tied to this model as an ABI if we decide to change.

When constructing dom0 it will therefore be necessary to rewrite any DTS properties which refer to an ITS to point to the single provided ITS, as well as dropping all ITS nodes and replacing them with a single node representing the vITS.

## Mapping from vLPI back to pLPI

While we have arranged for a (pDevice,pEvent) to map to a single pLPI we cannot guarantee that a given vLPI is mapped by a single (vDevice,vEvent) since the guest may setup multiple ITT tables such that this is not the case. Enforcing that this is the case is prohibitively expensive.

Therefore it is not in general possible to associate a vLPI with a pLPI.

## Per-domain struct pending_irq for vLPIs

Internally Xen uses a struct pending_irq to track the status of any pending virtual IRQ, including a virtual LPI.

Upon domain creation an array of such struct pending_irq's will be allocated to cover the range 8192..nr_lpis (for the number of LPIs which the guest is configured with) and a pointer this array will be stored in the struct domain. The function irq_to_pending will be modified to lookup interupts in the LPI range in this array.

## Handling of unrouted/spurious LPIs

Since there is no 1:1 link between a vLPI and pLPI enabling and disabling of physical LPIs cannot be driven from the state of an associated vLPI.

Each pLPI is routed and enabled during device assignment, therefore it is possible to receive a physical LPI which has yet to be routed (via a vITS) to a vLPI.

Similarly if a guest routes multiple Events to a single vLPI the interrupt may already be pending when we attempt to deliver it.

Such pLPIs shall be ignored and left in the priority dropped state (per the read from GICC_IAR). They will not be EOI-d in order to avoid a possible interrupt storm.

On device deassignment (including as part of domain destroy) after resetting the device it will be necessary to EOI any interrupts in such a state by walking over all events in the corresponding struct its_device.

## Enabling and disabling LPIs

Two new functions vgic_enable_lpi and vgic_disable_lpi will be provided which are analogous to vgic_enable_irqs and vgic_disable_irqs but work for the LPI interface. (Alternatively, refactoring the existing functions to work for all caes would be acceptable too).

A vLPI which has not yet be enabled will automatically be queued, by the existing vgic injection machinery, until a call to vgic_enable_lpi is made (in response to a trapped access to the virtual cfg table).

## LPI Configuration Table Virtualisation

A guest's write accesses to its LPI Configuration Table (which is just an area of guest RAM which the guest has nominated) will be trapped to the hypervisor, using stage 2 MMU permissions, in order for changes to be propagated into the host interrupt configuration.

On write bit[0] of the written byte is the enable/disable state for the irq and is handled thus, for each byte in the written value:

    vlpi = lpi corresponding to byte offset (addr - table_base);

    pending_irq = irq_to_pending(current, vlpi);
    pending_irq->priority = byte & 0xfc; /* XXX: or byte >> 2 */

    if ( byte & 0x1 )
        vgic_enable_lpi(current, vlpi);
    else
        vgic_disable_lpi(current, vlpi);

Note that physical interrupts are always configured with a priority of GIC_PRI_IRQ, regardless of the priority of any virtual interrupt.

## LPI Pending Table Virtualisation

According to GIC spec 4.8.5 this table is not necessarily in sync and the mechanism to force a sync is IMPLEMENTATION DEFINED, hence we don't need to do anything.

## Device Table Virtualisation

The IPA, size and cacheability attributes of the guest device table will be recorded in struct domain upon write to GITS_BASER0.

In order to lookup an entry for device:

define {get,set}_vdevice_entry(domain, device, struct device_table *entry):
offset = device*sizeof(struct vdevice_table)
if offset > <DT size>: error

dt_entry = <DT base IPA> + device*sizeof(struct vdevice_table)
page = get_page_from_gfn(current->domain, dt_entry>>PAGE_SHIFT, &p2mt, P2M_ALLOC); if !page: error
if !page_is_ram(p2mt): put_page(page); error;

dt_mapping = map_domain_page(page)

if (set)
dt_mapping[<appropriate page offset from device>] = *entry;
else
*entry = dt_mapping[<appropriate page offset>];

unmap_domain_page(dt_mapping)
put_page(page)

Since everything is based upon IPA (guest addresses) a malicious guest can only reference its own RAM here.

## ITT Virtualisation

The location of a VITS will have been recorded in the domain Device Table by a MAPI or MAPVI command and is looked up as above.

The vitt is a struct vitt:

    struct vitt {
        uint16_t valid:1;
        uint16_t pad:15;
        uint16_t vcollection;
        uint32_t vlpi;
    };
    BUILD_BUG_ON(sizeof(struct vitt) != 8);

A lookup occurs similar to for a device table, the offset is range checked against the vitt_size from the device table. To lookup event on device:

    define {get,set}_vitt_entry(domain, device, event, struct vitt *entry):
        get_vdevice_entry(domain, device, &dt)

        offset = event*sizeof(struct vitt);
        if offset > dt->vitt_size: error

        vitt_entry = dt->vita_ipa + event*sizeof(struct vitt)
        page = get_page_from_gfn(current->domain, vitt_entry>>PAGE_SHIFT, &p2mt, P2M_ALLOC);
        if !page: error
        if !page_is_ram(p2mt): put_page(page); error;

        vitt_mapping = map_domain_page(page)

        if (set)
             vitt_mapping[<appropriate page offset from event>] = *entry;
        else
             *entry = vitt_mapping[<appropriate page offset>];

        unmap_domain_page(entry)
        put_page(page)

Again since this is IPA based a malicious guest can only point things to its own ram.

## Collection Table Virtualisation

A pointer to a dynamically allocated array its_collections mapping collection ID to vcpu ID will be added to struct domain. The array shall have nr_vcpus + 1 entries and resets to ~0 (or another explicitly invalid vpcu nr).

## Virtual LPI injection

As discussed above the vgic_vcpu_inject_irq functionality will need to be extended to cover this new case, most likely via a new vgic_vcpu_inject_lpi frontend function. vgic_vcpu_inject_irq will also require some refactoring to allow the priority to be passed in from the caller (since LPI proprity comes from the LPI CFG table, while SPI and PPI priority is configured via other means).

vgic_vcpu_inject_lpi receives a struct domain * and a virtual interrupt number (corresponding to a vLPI) and needs to figure out which vcpu this should map to.

To do this it must look up the Collection ID associated (via the vITS) with that LPI.

Proposal: Add a new its_device field to struct irq_guest, a pointer to the associated struct its_device. The existing struct irq_guest.virq field contains the event ID (perhaps use a union to give a more appropriate name) and _not_ the virtual LPI. Injection then consists of:

        d = irq_guest->domain
        virq = irq_guest->virq
        its_device = irq_guest->its_device

        get_vitt_entry(d, its_device->virt_device_id, virq, &vitt)
        vcpu = d->its_collections[vitt.vcollection]

        if !is_valid_lpi(vitt.vlpi): error

        vgic_vcpu_inject_lpi(&d->vcpus[vcpu], vitt.vlpi)

If the LPI is currently disabled then it will be queued by vgic_vcpu_inject_lpi and injected in response to a subsequent vgic_enable_lpi call.

## Command Queue Virtualisation

The command translation/emulation in this design has been arranged to be as cheap as possible (e.g. in many cases the actions are NOPs), avoiding previous concerns about the length of time which an emulated write to a CWRITER register may block the vcpu.

Each domain will have an associated struct vits_cq:

    struct vits_cq {
        spinlock_t lock;
        atomic_t creadr;         /* Virtual creadr */
        [ Reference to command queue memory ]
    };

On read from GITS_CREADR the current value of vits_cq.creadr is read and returned using atomic_read. No lock is required.

Writes to GITS_CWRITER are handled synchronously as follows, given a write of val:

    spin_lock(vits_cq.lock)

    while vits_cq.creadr != val:
        process one command from offset vits_cq.creadr
        atomic_add(vits_cq.creadr, 0x10); /* XXX handle wrap around */

    spin_unlock(vits_cq.lock)
    increment PC
    return

Adding preemption to this is tricky with multiple vcpus present. e.g, a subtle case:

- VCPUA writes GITS_CWRITER W1, processes a proportion and is preempted.
- VCPUB write GITS_CWRITER W2 (>W1) and processes a proportion such that W1<GITS_CREADR<W2.
- VCPUA resumes, sees GITS_CREADR != W1, processes junk until it wraps around.

## ITS Command Translation

This section is based on the section 5.13 of GICv3 specification (PRD03-GENC-010745 24.0) and provides concrete ideas of how this can be interpreted for Xen.

The ITS provides 12 commands in order to manage interrupt collections, devices and interrupts. Possible command parameters are:

- Device ID (Device) (called device in the spec).
- Event ID (Event) (called ID in the spec). This is an index into a devices ITT.
- Collection ID (Collection) (called collection in the spec)
- LPI ID (LPI) (called pID in the spec)
- Target Address (TA) (called TA in the spec`)

These parameters need to be validated and translated from Virtual (v prefix) to Physical (p prefix).

Note, we differ from the naming in the GIC spec for clarity, in particular we use Event not ID and LPI not pID to reduce confusion, especially when v and p suffixes are used due to virtualisation.

### Parameter Validation / Translation

Each command contains parameters that needs to be validated before any usage in Xen or passing to the hardware.

#### Device ID (Device)

Corresponding ITT obtained by looking up as described above.

The physical struct its_device can be found by looking up in the domain's device map.

If lookup fails or the resulting device table entry is invalid then the Device is invalid.

#### Event ID (Event)

Validated against emulated GITS_TYPER.IDbits.

It is not necessary to translate a vEvent.

#### LPI (LPI)

Validated against emulated GITS_TYPER.IDbits.

It is not necessary to translate a vLPI into a pLPI since the tables all contain vLPI. (Translation from pLPI to vLPI happens via struct irq_guest when we receive the IRQ).

#### Interrupt Collection (Collection)

The Collection is validated against the size of the per-domain its_collections array (i.e. nr_vcpus + 1) and then translated by a simple lookup in that array.

     vcpu_nr = d->its_collections[Collection]

A result > nr_cpus is invalid

#### Target Address (TA)

This parameter is used in commands which manage collections. It is a unique identifier per processor.

We have chosen to implement GITS_TYPER.PTA as 0, hence vTA simply corresponds to the vcpu_id, so only needs bounds checking against nr_vcpus.

### Commands

To be read with reference to spec for each command (which includes error checks etc which are omitted here).

It is assumed that inputs will be bounds and validity checked as described above, thus error handling is omitted for brevity (i.e. if get and/or set fail then so be it). In general invalid commands are simply ignored.

#### MAPD: Map a physical device to an ITT.

_Format_: MAPD Device, Valid, ITT Address, ITT Size.

_Spec_: 5.13.11

MAPD is sent with Valid bit set if the mapping is to be added and reset when mapping is removed.

When the Valid bit is set then the range ITT Address to ITT Address + ITT Size need not be validated, this is done in {get,set}_vdevice_entry when calling the get_page_from_gfn function. Validating the memory at MAPD time would serve no purpose since the guest could subsequently balloon it out or grant map over it etc.

The domain's device table is updated with the provided information.

The vitt_mapd field is set according to the Valid flag in the command:

dt_entry.vitt_ipa = ITT Address dt_entry.vitt_size = ITT Size set_vdevice_entry(current->domain, Device, &dt_entry)

#### MAPC: Map an interrupt collection to a target processor

_Format_: MAPC Collection, TA

_Spec_: 5.13.12

The updated vTA (a vcpu number) is recorded in the its_collections array of the domain struct:

    d->its_collections[Collection] = TA

#### MAPI: Map an interrupt to an interrupt collection.

_Format_: MAPI Device, LPI, Collection

_Spec_: 5.13.13

After validation:

vitt.valid = True
vitt.collection = Collection
vitt.vlpi = LPI set_vitt_entry(current->domian, Device, LPI, &vitt)

#### MAPVI: Map an input identifier to a physical interrupt and an interrupt collection.

Format: MAPVI Device, Event, LPI, Collection

vitt.valid = True
vitt.collection = Collection
vitt.vlpi = LPI set_vitt_entry(current->odmian, Device, Event, &vitt)

#### MOVI: Redirect interrupt to an interrupt collection

_Format_: MOVI Device, Event, Collection

_Spec_: 5.13.15

get_vitt_entry(current->domain, Device, Event, &vitt) vitt.collection = Collection set_vitt_entry(current->domain, Device, Event, &vitt)

XXX consider helper which sets field without mapping/unmapping twice.

This command is supposed to move any pending interrupts associated with Event to the vcpu implied by the new Collection, which is tricky. For now we ignore this requirement (as we do for GICD_IROUTERn and GICD_TARGETRn for other interrupt types).

#### DISCARD: Discard interrupt requests

_Format_: DISCARD Device, Event

_Spec_: 5.13.16

get_vitt_entry(current->domain, Device, Event, &vitt) vitt.valid = False set_vitt_entry(current->domain, Device, Event, &vitt)

XXX consider helper which sets field without mapping/unmapping twice.

This command is supposed to clear the pending state of any associated interrupt. This requirement is ignored (guest may see a spurious interrupt).

#### INV: Clean any caches associated with interrupt

_Format_: INV Device, Event

_Spec_: 5.13.17

Since LPI Configuration table updates are handled synchronously, there is nothing to do here.

#### INVALL: Clean any caches associated with an interrupt collection

_Format_: INVALL Collection

_Spec_: 5.13.19

Since LPI Configuration table updates are handled synchronously, there is nothing to do here.

#### INT: Generate an interrupt

_Format_: INT Device, Event

_Spec_: 5.13.20

The vitt entry corresonding to Device,Event is looked up and then:

    get_vitt_entry(current->domain, Device, Event, &vitt)
    vgic_vcpu_inject_lpi(current->domain, vitt.vlpi)

_Note_: Where (Device,Event) is real may need consideration of interactions with real LPIs being delivered: Julien had concerns about Xen's internal IRQ State tracking. if this is a problem then may need changes to IRQ state tracking, or to inject as a real IRQ and let physical IRQ injection handle it, or write to GICR_SETLPIR?

#### CLEAR: Clear the pending state of an interrupt

_Format_: CLEAR Device, Event

_Spec_: 5.13.21

Should clear pending state of LPI. Ignore (guest may see a spurious interrupt).

#### SYNC: Wait for completion of any outstanding ITS actions for collection

_Format_: SYNC TA

_Spec_: 5.13.22

This command can be ignored.

# GICv4 Direct Interrupt Injection

GICv4 will directly mark the LPIs pending in the virtual pending table which is per-redistributor (i.e per-vCPU).

LPIs will be received by the guest the same way as an SPIs. I.e trap in IRQ mode then read ICC_IAR1_EL1 (for GICv3).

Therefore GICv4 will not require one vITS per pITS.

# Event Channels

It has been proposed that it might be nice to inject event channels as LPIs in the future. Whether or not that would involve any sort of vITS is unclear, but if it did then it would likely be a separate emulation to the vITS emulation used with a pITS and as such is not considered further here.

# Glossary

* _MSI_: Message Signalled Interrupt
* _ITS_: Interrupt Translation Service
* _GIC_: Generic Interrupt Controller
* _LPI_: Locality-specific Peripheral Interrupt

# References

"GIC Architecture Specification" PRD03-GENC-010745 24.0.

"IO Remapping Table System Software on ARM® Platforms" ARM DEN 0049A.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#87 lore raw
In reply to [email protected]
Quoted 11 lines · On Mon, 2015-06-22 at 17:31 +0530, [email protected] wrote:

Vijaya Kumar K (18):
xen/arm: Add bitmap_find_next_zero_area helper function
xen: Add log2 functionality
xen: console: Add ratelimit support for error message xen/arm: gicv3: Refactor redistributor information xen/arm: ITS: Port ITS driver to xen
xen/arm: ITS: Add helper functions to manage its_devices xen/arm: ITS: implement hw_irq_controller for LPIs xen/arm: vITS: Add virtual ITS driver
xen/arm: ITS: Add virtual ITS commands support xen/arm: ITS: Add APIs to add and assign device

Quoted 8 lines

xen/arm: ITS: Add GITS registers emulation xen/arm: ITS: Add GICR register emulation xen/arm: ITS: Add irq descriptors for LPIs xen/arm: ITS: Initialize physical ITS xen/arm: ITS: Add domain specific ITS initialization xen/arm: ITS: Handle LPI interrupts xen/arm: ITS: Generate ITS node for Dom0 xen/arm: ITS: Map ITS translation space

FWIW I think it would be more logical to order this as:
* Preparation, precursor cleanups etc
* PITS support
* VITS support, which uses pits stuff
* Domain vits init, including map the ITS translation page, which uses vits stuff
* Device Tree mangling which finally exposes the result to the guest.

But I think far more critical would be to make function which whould eventually be static be so when it is introduced (not later) and to include prototypes of functions in the patch which adds the function. Also lots more const correctness would be good.

Ian.