Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * dice_transaction.c - a part of driver for Dice based devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (c) Clemens Ladisch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (c) 2014 Takashi Sakamoto
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include "dice.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) static u64 get_subaddr(struct snd_dice *dice, enum snd_dice_addr_type type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 		       u64 offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 	switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 	case SND_DICE_ADDR_TYPE_TX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 		offset += dice->tx_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	case SND_DICE_ADDR_TYPE_RX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 		offset += dice->rx_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	case SND_DICE_ADDR_TYPE_SYNC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 		offset += dice->sync_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	case SND_DICE_ADDR_TYPE_RSRV:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 		offset += dice->rsrv_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	case SND_DICE_ADDR_TYPE_GLOBAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 		offset += dice->global_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	offset += DICE_PRIVATE_SPACE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	return offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) int snd_dice_transaction_write(struct snd_dice *dice,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 			       enum snd_dice_addr_type type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 			       unsigned int offset, void *buf, unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	return snd_fw_transaction(dice->unit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 				  (len == 4) ? TCODE_WRITE_QUADLET_REQUEST :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 					       TCODE_WRITE_BLOCK_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 				  get_subaddr(dice, type, offset), buf, len, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) int snd_dice_transaction_read(struct snd_dice *dice,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 			      enum snd_dice_addr_type type, unsigned int offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 			      void *buf, unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	return snd_fw_transaction(dice->unit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 				  (len == 4) ? TCODE_READ_QUADLET_REQUEST :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 					       TCODE_READ_BLOCK_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 				  get_subaddr(dice, type, offset), buf, len, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) static unsigned int get_clock_info(struct snd_dice *dice, __be32 *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	return snd_dice_transaction_read_global(dice, GLOBAL_CLOCK_SELECT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 						info, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) int snd_dice_transaction_get_clock_source(struct snd_dice *dice,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 					  unsigned int *source)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	__be32 info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	err = get_clock_info(dice, &info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	if (err >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		*source = be32_to_cpu(info) & CLOCK_SOURCE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) int snd_dice_transaction_get_rate(struct snd_dice *dice, unsigned int *rate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	__be32 info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	unsigned int index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	err = get_clock_info(dice, &info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	index = (be32_to_cpu(info) & CLOCK_RATE_MASK) >> CLOCK_RATE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	if (index >= SND_DICE_RATES_COUNT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		err = -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	*rate = snd_dice_rates[index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) int snd_dice_transaction_set_enable(struct snd_dice *dice)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	__be32 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	if (dice->global_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	value = cpu_to_be32(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	err = snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 				 get_subaddr(dice, SND_DICE_ADDR_TYPE_GLOBAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 					     GLOBAL_ENABLE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 				 &value, 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 				 FW_FIXED_GENERATION | dice->owner_generation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	dice->global_enabled = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) void snd_dice_transaction_clear_enable(struct snd_dice *dice)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	__be32 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 			   get_subaddr(dice, SND_DICE_ADDR_TYPE_GLOBAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 				       GLOBAL_ENABLE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			   &value, 4, FW_QUIET |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 			   FW_FIXED_GENERATION | dice->owner_generation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	dice->global_enabled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static void dice_notification(struct fw_card *card, struct fw_request *request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 			      int tcode, int destination, int source,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			      int generation, unsigned long long offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 			      void *data, size_t length, void *callback_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	struct snd_dice *dice = callback_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	u32 bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	if (tcode != TCODE_WRITE_QUADLET_REQUEST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		fw_send_response(card, request, RCODE_TYPE_ERROR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	if ((offset & 3) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		fw_send_response(card, request, RCODE_ADDRESS_ERROR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	bits = be32_to_cpup(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	spin_lock_irqsave(&dice->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	dice->notification_bits |= bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	spin_unlock_irqrestore(&dice->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	fw_send_response(card, request, RCODE_COMPLETE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	if (bits & NOTIFY_LOCK_CHG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		complete(&dice->clock_accepted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	wake_up(&dice->hwdep_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) static int register_notification_address(struct snd_dice *dice, bool retry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	struct fw_device *device = fw_parent_device(dice->unit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	__be64 *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	unsigned int retries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	retries = (retry) ? 3 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	buffer = kmalloc(2 * 8, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	if (!buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		buffer[0] = cpu_to_be64(OWNER_NO_OWNER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		buffer[1] = cpu_to_be64(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 			((u64)device->card->node_id << OWNER_NODE_SHIFT) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 			dice->notification_handler.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		dice->owner_generation = device->generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		smp_rmb(); /* node_id vs. generation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		err = snd_fw_transaction(dice->unit, TCODE_LOCK_COMPARE_SWAP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 					 get_subaddr(dice,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 						     SND_DICE_ADDR_TYPE_GLOBAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 						     GLOBAL_OWNER),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 					 buffer, 2 * 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 					 FW_FIXED_GENERATION |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 							dice->owner_generation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		if (err == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 			/* success */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 			if (buffer[0] == cpu_to_be64(OWNER_NO_OWNER))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 			/* The address seems to be already registered. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 			if (buffer[0] == buffer[1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 			dev_err(&dice->unit->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 				"device is already in use\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 			err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		if (err != -EAGAIN || retries-- > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		msleep(20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		dice->owner_generation = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) static void unregister_notification_address(struct snd_dice *dice)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	struct fw_device *device = fw_parent_device(dice->unit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	__be64 *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	buffer = kmalloc(2 * 8, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	if (buffer == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	buffer[0] = cpu_to_be64(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		((u64)device->card->node_id << OWNER_NODE_SHIFT) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		dice->notification_handler.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	buffer[1] = cpu_to_be64(OWNER_NO_OWNER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	snd_fw_transaction(dice->unit, TCODE_LOCK_COMPARE_SWAP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 			   get_subaddr(dice, SND_DICE_ADDR_TYPE_GLOBAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 				       GLOBAL_OWNER),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 			   buffer, 2 * 8, FW_QUIET |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 			   FW_FIXED_GENERATION | dice->owner_generation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	dice->owner_generation = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) void snd_dice_transaction_destroy(struct snd_dice *dice)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	struct fw_address_handler *handler = &dice->notification_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	if (handler->callback_data == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	unregister_notification_address(dice);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	fw_core_remove_address_handler(handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	handler->callback_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) int snd_dice_transaction_reinit(struct snd_dice *dice)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	struct fw_address_handler *handler = &dice->notification_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	if (handler->callback_data == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	return register_notification_address(dice, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) static int get_subaddrs(struct snd_dice *dice)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	static const int min_values[10] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		10, 0x60 / 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		10, 0x18 / 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		10, 0x18 / 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	__be32 *pointers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	__be32 version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	u32 data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	pointers = kmalloc_array(ARRAY_SIZE(min_values), sizeof(__be32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 				 GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	if (pointers == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	 * Check that the sub address spaces exist and are located inside the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	 * private address space.  The minimum values are chosen so that all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	 * minimally required registers are included.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	err = snd_fw_transaction(dice->unit, TCODE_READ_BLOCK_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 				 DICE_PRIVATE_SPACE, pointers,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 				 sizeof(__be32) * ARRAY_SIZE(min_values), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	for (i = 0; i < ARRAY_SIZE(min_values); ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		data = be32_to_cpu(pointers[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		if (data < min_values[i] || data >= 0x40000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 			err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 			goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	if (be32_to_cpu(pointers[1]) > 0x18) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		 * Check that the implemented DICE driver specification major
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		 * version number matches.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		err = snd_fw_transaction(dice->unit, TCODE_READ_QUADLET_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 				DICE_PRIVATE_SPACE +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 				be32_to_cpu(pointers[0]) * 4 + GLOBAL_VERSION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 				&version, sizeof(version), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 			goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		if ((version & cpu_to_be32(0xff000000)) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 						cpu_to_be32(0x01000000)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 			dev_err(&dice->unit->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 				"unknown DICE version: 0x%08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 				be32_to_cpu(version));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 			err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 			goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		/* Set up later. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		dice->clock_caps = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	dice->global_offset = be32_to_cpu(pointers[0]) * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	dice->tx_offset = be32_to_cpu(pointers[2]) * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	dice->rx_offset = be32_to_cpu(pointers[4]) * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	/* Old firmware doesn't support these fields. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	if (pointers[7])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		dice->sync_offset = be32_to_cpu(pointers[6]) * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	if (pointers[9])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		dice->rsrv_offset = be32_to_cpu(pointers[8]) * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	kfree(pointers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) int snd_dice_transaction_init(struct snd_dice *dice)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	struct fw_address_handler *handler = &dice->notification_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	err = get_subaddrs(dice);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	/* Allocation callback in address space over host controller */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	handler->length = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	handler->address_callback = dice_notification;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	handler->callback_data = dice;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	err = fw_core_add_address_handler(handler, &fw_high_memory_region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		handler->callback_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	/* Register the address space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	err = register_notification_address(dice, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		fw_core_remove_address_handler(handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		handler->callback_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) }