Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 73 additions & 10 deletions drivers/soc/apple/diag_shim.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
#include <linux/bitops.h>
#include <linux/ctype.h>
#include <linux/device.h>
#include <linux/kernfs.h>
#include <linux/ktime.h>
#include <linux/minmax.h>
#include <linux/spinlock.h>
#include <linux/string.h>
#include <linux/sysfs.h>

Expand Down Expand Up @@ -118,8 +120,39 @@ static struct {
atomic_t unhandled;
atomic_t sleeps;
atomic_t sleeping;

struct kernfs_node *keystore_kn;
struct kernfs_node *touchid_kn;
} sep_diag;

static DEFINE_SPINLOCK(sep_diag_kn_lock);

static void sep_diag_put_kns(void *data)
{
struct kernfs_node *keystore_kn, *touchid_kn;
unsigned long flags;

spin_lock_irqsave(&sep_diag_kn_lock, flags);
keystore_kn = sep_diag.keystore_kn;
touchid_kn = sep_diag.touchid_kn;
sep_diag.keystore_kn = NULL;
sep_diag.touchid_kn = NULL;
spin_unlock_irqrestore(&sep_diag_kn_lock, flags);

sysfs_put(keystore_kn);
sysfs_put(touchid_kn);
}

static void sep_diag_notify_kn(struct kernfs_node **knp)
{
unsigned long flags;

spin_lock_irqsave(&sep_diag_kn_lock, flags);
if (*knp)
kernfs_notify(*knp);
spin_unlock_irqrestore(&sep_diag_kn_lock, flags);
}

/* Written once per probe, before pnon reads SEP_DIAG_PNON_READ. */
static u8 sep_diag_pnon_digest[SEP_DIAG_PNON_DIGEST_LEN];

Expand Down Expand Up @@ -735,6 +768,10 @@ static const struct attribute_group sep_diag_group = {
int sep_diag_register(struct device *dev, const char *profile,
size_t profile_len, bool cold, bool sepos13, bool xart)
{
struct kernfs_node *diag_kn, *keystore_kn = NULL, *touchid_kn = NULL;
unsigned long flags;
int ret;

profile_len = min(profile_len, sizeof(sep_diag.profile) - 1);
memcpy(sep_diag.profile, profile, profile_len);
sep_diag.profile[profile_len] = '\0';
Expand All @@ -743,7 +780,23 @@ int sep_diag_register(struct device *dev, const char *profile,
sep_diag.xart = xart;
sep_diag.probe_ns = ktime_get_boottime_ns();

return devm_device_add_group(dev, &sep_diag_group);
ret = devm_device_add_group(dev, &sep_diag_group);
if (ret)
return ret;

diag_kn = sysfs_get_dirent(dev->kobj.sd, "diag");
if (diag_kn) {
keystore_kn = sysfs_get_dirent(diag_kn, "keystore");
touchid_kn = sysfs_get_dirent(diag_kn, "touchid");
sysfs_put(diag_kn);
}

spin_lock_irqsave(&sep_diag_kn_lock, flags);
sep_diag.keystore_kn = keystore_kn;
sep_diag.touchid_kn = touchid_kn;
spin_unlock_irqrestore(&sep_diag_kn_lock, flags);

return devm_add_action_or_reset(dev, sep_diag_put_kns, NULL);
}

void sep_diag_set_attach(bool attached)
Expand Down Expand Up @@ -867,7 +920,9 @@ void sep_diag_log_summary(struct device *dev)

void sep_diag_set_keystore_open(void)
{
atomic_set(&sep_diag.keystore, SEP_DIAG_KEYSTORE_OPEN);
if (atomic_xchg(&sep_diag.keystore, SEP_DIAG_KEYSTORE_OPEN) !=
SEP_DIAG_KEYSTORE_OPEN)
sep_diag_notify_kn(&sep_diag.keystore_kn);
}

void sep_diag_set_keybag(int state)
Expand Down Expand Up @@ -925,14 +980,18 @@ void sep_diag_set_sensor_result(unsigned int bind, bool online)
*/
void sep_diag_set_bio_published(void)
{
atomic_cmpxchg(&sep_diag.touchid, SEP_DIAG_TOUCHID_PENDING,
SEP_DIAG_TOUCHID_PUBLISHED);
if (atomic_cmpxchg(&sep_diag.touchid, SEP_DIAG_TOUCHID_PENDING,
SEP_DIAG_TOUCHID_PUBLISHED) ==
SEP_DIAG_TOUCHID_PENDING)
sep_diag_notify_kn(&sep_diag.touchid_kn);
}

void sep_diag_set_touchid(bool started)
{
atomic_set(&sep_diag.touchid,
started ? SEP_DIAG_TOUCHID_STARTED : SEP_DIAG_TOUCHID_FAILED);
int state = started ? SEP_DIAG_TOUCHID_STARTED : SEP_DIAG_TOUCHID_FAILED;

if (atomic_xchg(&sep_diag.touchid, state) != state)
sep_diag_notify_kn(&sep_diag.touchid_kn);
}

void sep_diag_set_sensor_attach(int state)
Expand Down Expand Up @@ -978,13 +1037,17 @@ void sep_diag_set_xart(bool writable)
void sep_diag_keystore_ended(void)
{
atomic64_cmpxchg(&sep_diag.ready_ns, 0, ktime_get_boottime_ns());
atomic_cmpxchg(&sep_diag.keystore, SEP_DIAG_KEYSTORE_UNKNOWN,
SEP_DIAG_KEYSTORE_CLOSED);
if (atomic_cmpxchg(&sep_diag.keystore, SEP_DIAG_KEYSTORE_UNKNOWN,
SEP_DIAG_KEYSTORE_CLOSED) ==
SEP_DIAG_KEYSTORE_UNKNOWN)
sep_diag_notify_kn(&sep_diag.keystore_kn);
}

void sep_diag_bringup_ended(void)
{
sep_diag_keystore_ended();
atomic_cmpxchg(&sep_diag.touchid, SEP_DIAG_TOUCHID_PENDING,
SEP_DIAG_TOUCHID_UNAVAILABLE);
if (atomic_cmpxchg(&sep_diag.touchid, SEP_DIAG_TOUCHID_PENDING,
SEP_DIAG_TOUCHID_UNAVAILABLE) ==
SEP_DIAG_TOUCHID_PENDING)
sep_diag_notify_kn(&sep_diag.touchid_kn);
}
60 changes: 57 additions & 3 deletions tools/aurora-sep/tests/test_diag_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,31 @@
return (u64)now.tv_sec * 1000000000ULL + now.tv_nsec;
}
static inline u64 div_u64(u64 value, u32 divisor) { return value / divisor; }
''',
'linux/kernfs.h': r'''
#include <linux/types.h>
struct kernfs_node {
const char *name;
struct kernfs_node *const *children;
unsigned int notifies;
};
static inline void kernfs_notify(struct kernfs_node *kn)
{
if (kn)
kn->notifies++;
}
''',
'linux/spinlock.h': r'''
typedef struct { int unused; } spinlock_t;
#define DEFINE_SPINLOCK(x) spinlock_t x
#define spin_lock_irqsave(lock, flags) do { (void)(lock); (flags) = 0; } while (0)
#define spin_unlock_irqrestore(lock, flags) do { (void)(lock); (void)(flags); } while (0)
''',
'linux/device.h': r'''
#include <linux/types.h>
struct device { int unused; };
struct kernfs_node;
struct kobject { struct kernfs_node *sd; };
struct device { struct kobject kobj; };
#define dev_info(dev, ...) ((void)(dev))
struct attribute { const char *name; umode_t mode; };
struct device_attribute {
Expand All @@ -118,11 +139,32 @@
registered_group = grp;
return 0;
}
static inline int devm_add_action_or_reset(struct device *dev,
void (*action)(void *), void *data)
{
(void)dev;
(void)action;
(void)data;
return 0;
}
''',
'linux/sysfs.h': r'''
#include <linux/kernfs.h>
#include <linux/types.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
static inline struct kernfs_node *sysfs_get_dirent(struct kernfs_node *parent,
const char *name)
{
if (!parent || !parent->children)
return NULL;
for (struct kernfs_node *const *c = parent->children; *c; c++)
if (!strcmp((*c)->name, name))
return *c;
return NULL;
}
static inline void sysfs_put(struct kernfs_node *kn) { (void)kn; }
static inline int sysfs_emit(char *buf, const char *fmt, ...)
{
va_list args;
Expand Down Expand Up @@ -160,6 +202,7 @@
#include <stdlib.h>
#include <string.h>
#include <linux/types.h>
#include <linux/kernfs.h>
#include <linux/device.h>
#include "shim.h"

Expand Down Expand Up @@ -215,7 +258,13 @@

int main(int argc, char **argv)
{
struct device dev;
struct kernfs_node keystore_kn = { .name = "keystore" };
struct kernfs_node touchid_kn = { .name = "touchid" };
struct kernfs_node *const diag_children[] = { &keystore_kn, &touchid_kn, NULL };
struct kernfs_node diag_kn = { .name = "diag", .children = diag_children };
struct kernfs_node *const root_children[] = { &diag_kn, NULL };
struct kernfs_node root_kn = { .name = "root", .children = root_children };
struct device dev = { .kobj = { .sd = &root_kn } };
unsigned int bind, stale;

/* The Neo: warm attach, xART writable, no guard. */
Expand Down Expand Up @@ -245,6 +294,10 @@
expect("attach", "sensor_attach", "attached");
expect("attach", "restore", "restored");
expect("attach", "touchid", "ready");
if (touchid_kn.notifies != 2) {
printf("FAIL attach: touchid notifies is %u, want 2\n", touchid_kn.notifies);
failures++;
}
if (argc > 1)
dump(argv[1]);

Expand Down Expand Up @@ -285,6 +338,7 @@
#include <stdlib.h>
#include <string.h>
#include <linux/types.h>
#include <linux/kernfs.h>
#include <linux/device.h>
#include "shim.h"

Expand All @@ -310,7 +364,7 @@
/* argv: level list matching-at-probe refusals matching-after-refusal */
int main(int argc, char **argv)
{
struct device dev;
struct device dev = { 0 };
int refusals = atoi(argv[4]);

sep_diag_set_xart_guard(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]));
Expand Down
Loading